Discrete Mathematics in action in R

BioCodeKb - Bioinformatics Knowledgebase

An operator in R is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and has many operators to carry out different mathematical and logical operations.

Operators in R can mainly be classified into the following categories.

R Arithmetic Operators

These operators are used to carry out mathematical operations like addition and multiplication.

R Relational Operators

Relational operators are used to compare between values. Relational Operators are also known as comparators which help us to see how one R-Object relates to another R-object. For example, we can check whether two objects are equal or not, which can be accomplished with the help of == (double equal) sign. A logical operator which is TRUE on both sides, will return a logical value as TRUE since TRUE equals TRUE.

Precedence of relational operators is lower than arithmetic operators. To avoid confusion, it is good practice to use brackets to control ordering.

R Logical Operators

Logical operators are used to carry out Boolean operations like AND, OR etc.

Operators & and | perform element-wise operation producing result having length of the longer operand.

But && and || examines only the first element of the operands resulting into a single length logical vector. Zero is considered FALSE and non-zero numbers are taken as TRUE.

The basic way to define a logical variable is by relational operators by comparing two expressions. For example, we often ask if a variable x is bigger than a certain number?

A more sophisticated way is to combine two simple logical statements using logical operators.

It compares two numerical expressions and returns a Boolean variable: either 0 (FALSE) or 1 (TRUE). In practice, we often use two or more conditions to decide what to do. For example, scholarship is often given if the candidate has done well in both academic and extra-curricular activities.

To combine different conditions, there are three logical operators:

  1. &&

  2. ||

  3. !

First, && is similar to AND in English, that is, A && B is true only if both A and B are true. 

Second, is similar to OR in English, that is, A || B is false only if both A and B are false. Third, ! is similar to NOT in English, that is, !A is true only if A is false.

Different relational operators, the precedence of logical operators can be high. While and and or operators are lower in precedence than relational operators, not has a very high precedence and almost always evaluated first. - Hence, it is always a good practice to use brackets to control operation ordering.

R Assignment Operators

These operators are used to assign values to variables. The operators <- and = can be used, almost interchangeably, to assign to variable in the same environment.

The <<- operator is used for assigning to variables in the parent environments (more like global assignments). The rightward assignments, although available are rarely used.

Similarly, the inequality operator can also be used for numerical and other R-Objects like matrices, vectors, and lists.

  • What if you want to check whether given two R-Objects which one of them is greater or less than the other. Well, of course, we can, using simple < and > sign (less-than and greater-than sign).

Conditional Statements

R provides us a way to combine all of these operators and use the result to change the behavior of our R scripts.

The syntax of the if condition is defined below:

if(condition){ // expression }

The if statement takes an input a condition ( inside parentheses ) which we can relate to a relational or some logical operator and if that condition is TRUE then the R code associated with the condition ( inside curly brackets ) is executed.

Operation on Vectors

The above mentioned operators work on vectors. We can use the function c() (as in concatenate) to make vectors in R.

All operations are carried out in element-wise fashion. When there is a mismatch in length (number of elements) of operand vectors, the elements in shorter one is recycled in a cyclic manner to match the length of the longer one. R will issue a warning if the length of the longer vector is not an integral multiple of the shorter vector.

Need to learn more about Discrete Mathematics in action in R and much more?

To learn Bioinformatics, analysis, tools, biological databases, Computational Biology, Bioinformatics Programming in Python & R through interactive video courses and tutorials, Join BioCode.

Get in touch with us

Tel: +44 7518 945488

Email: Contact@BioCode.ltd

  • Black Instagram Icon
  • Facebook


4 Mann Island, Liverpool, Merseyside, United Kingdom

Qurtaba Heights, Islamabad, Pakistan

© Copyright 2020 BioCode Ltd. - All rights reserved.