7  Operators in R

Let us learn various operators in R.

7.1 Assignment Operators

So far we have been using = notation to assgin values to an object. For example age = c(10, 11, 12) indicates we are storing the age of three individuals to an object age with the assignment sign =. R has another way to assign object to its values.

Assignment Operators in R
Operator Description Example
= Assign the value to the object on the left x = 10
<- Assign the value to the object on the left x <- 10
-> Assign the value to the object on the right 10 -> x

We mostly use the = or the <- operators to assign values to ab object. It is recommended to use <- operator to avoid any confusion in cases such as the following.

x = 5
x = x == 3 # checking if the value of x is 3
x
[1] FALSE

Much readable with <- operator

x = 5
x <- x == 3 # checking if the value of x is 3
x
[1] FALSE

7.2 Arithmetic Operators

These are the operators for performing computation with numerical data type

Arithmetic Operators in R
Operator Description Sample Code Output
+ Addition 10 + 10 20
- Subtraction 10 - 8 2
* Multiplication 5 * 10 50
/ Division 50 / 10 5
^ Exponent 10 ^ 2 100
%/% Integer division 20 %/% 3 6
%% Modulus (remainder) 10 %% 3 1
%*% Matrix multiplication see below see below

Let us define the two matrices as follows

mat_a = matrix(c(10, 20, 30, 40), nrow = 2)
mat_b = matrix(c(1, 2, 3, 4), nrow = 2)

mat_a
     [,1] [,2]
[1,]   10   30
[2,]   20   40
mat_b
     [,1] [,2]
[1,]    1    3
[2,]    2    4
# Element wise multiplication
mat_a * mat_b
     [,1] [,2]
[1,]   10   90
[2,]   40  160
# True matrix multiplication
mat_a %*% mat_b
     [,1] [,2]
[1,]   70  150
[2,]  100  220

7.3 Relational Operators

Relational operators return boolean output.

Relational Operators in R
Operator Description Example Output
> Greater than 5 > 2 TRUE
< Less than 5 < 2 FALSE
>= Greater than or equal to 10 >= 10 TRUE
c(1, 10) >= c(-2, 20) TRUE, FALSE
<= Less than or equal to 100 <= 100 TRUE
c(1, 10) <= c(-2, 20) FALSE, TRUE
== Equal to 10 == 10 TRUE
'Male' == 'M' FALSE
'Male' == ' Male' FALSE
'Male' == 'male' FALSE
!= Not equal to 10 != 9 TRUE
'Male' != 'male' TRUE

7.4 Logical Operators

Logical operators also return boolean values TRUE or FALSE

Logical Operators in R
Operator Description Example Output
! Logical NOT
& Element-wise Logical AND
| Element-wise Logical OR
&& Logical AND
|| Logical OR

For the && and || operators, I will not discuss here. But if you are interested, read the documentation which reads.

The “short-circuit” operators && and || are often used as part of the condition in an if statement. Whereas & and | apply element-wise to vectors, && and || apply to vectors of length one, and only evaluate their second argument if necessary.

The full R official documentation can be accessed here.

7.5 Examples of logical operators

To negate the condition, we prefix with the ! sign. != means not equal to. That is, we are negating the equality. Lets see some examples

x = 3
x == 3 # its TRUE
[1] TRUE
!(x == 3) # when negated, it should be false
[1] FALSE
x != 3 # equivalent to the previous one
[1] FALSE

7.6 Element-wise Logical AND example

7.7 Logical AND

df = data.frame(
    serial = 1:5,
  age = c(10, 11, 20, 30, 32), 
  sex = c('M', 'F', 'F', 'M', 'M')
)

df
  serial age sex
1      1  10   M
2      2  11   F
3      3  20   F
4      4  30   M
5      5  32   M
df$age == 10 & df$sex == 'M'
[1]  TRUE FALSE FALSE FALSE FALSE

References