R-operators
R-operators

R Operators

Posted on

An operator is a symbol that asks the compiler to perform specific tasks of mathematical or logical manipulations. R language provides many types of operators.

Types of Operators in R

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Miscellaneous Operators

Arithmetic Operators in R

These operators are used to carry out mathematical operations such as addition or multiplication.

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
^Exponent
%%Modulus (Remainder from division)
%/%Integer Division

Example:

x <- 7
y <- 12
x + y
x - y
x * y
y / x
y %/% x
y %% x
y ^ x

#output:
[1] 19
[1] -5
[1] 84
[1] 1.714286
[1] 1
[1] 5
[1] 35831808

Relational Operators in R

Relational operators are used to compare between values.

OperatorDescription
<less than
<=less than or equal to
>greater than
>=greater than or equal to
==exactly equal to
!=not equal to

Example:

x <- 7
y <- 15
x < y
x > y
x <= 7
y >= 21
y == 15
x != 7

#output:
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE

Operation on Vectors

The operators above work on vectors. The variables used above were in fact single element vectors. We could use the function c() (as in concatenate) to create vectors in R.

Example:

x <- c(1, 7, 4)
y <- c(5, 3, 2)
x + y
x > y

#output:
[1]  6 10  6
[1] FALSE  TRUE  TRUE

R will issue a warning if the longer object length is not a multiple of shorter object length of the vector.

x <- c(5, 2, 9, 3)
y <- c(7, 1)
x + y
x - 1
x + c(1, 2, 3)

#output:
[1] 12  3 16  4
[1] 4 1 8 2
[1]  6  4 12  4
Warning message:
In x + c(1, 2, 3) :
  longer object length is not a multiple of shorter object length

Logical Operators in R

Logical operators are used to carry out Boolean operations such as ANDOR, NOT etc.

OperatorDescription
!Logical NOT
&Element-wise logical AND
&&Logical AND
|Element-wise logical OR
||Logical OR

‘!’ is called Logical NOT operator. It takes gives the opposite logical value of the vector.
‘&’ is called Element-wise Logical AND operator. It combines the the first vector element with the corresponding second vector element and gives a output TRUE if both the elements are TRUE.
‘|’ is called Element-wise Logical OR operator. It combines the first vector with the corresponding second vector and gives a output TRUE if one the elements is TRUE.
‘&&’ and ‘||’ considers only the first element of the vectors and give a vector of single element as output.

Example:

x <- c(TRUE, FALSE, 0, 7)
y <- c(FALSE, TRUE, FALSE, TRUE)
!x
x & y
x | y
x && y
x || y

#output:
[1] FALSE  TRUE  TRUE FALSE
[1] FALSE FALSE FALSE  TRUE
[1]  TRUE  TRUE FALSE  TRUE
Error in x || y : 'length = 4' in coercion to 'logical(1)'
Error in x || y : 'length = 4' in coercion to 'logical(1)'

There are changes since R 2.4.0 version so that calling ‘&&’ or ‘||’ with either argument of length greater than one now gives a warning (which it is intended will become an error).

Assignment Operators in R

These operators are used to assign values to variables.

OperatorDescription
<-, <<-Leftwards assignment
->, ->>Rightwards assignment

The operators <- and = could almost interchangeably, to assign to variables in the same environment. The <<- operator is used to assign to variables in the parent environments (more like global assignments). The ->>, although available, are rarely used.

Example:

a <- 5
a
b <<- 9
b
10 -> c
c
11 ->> d
d

#output
[1] 5
[1] 9
[1] 10
[1] 11

Miscellaneous Operators in R

These operators are used to for specific purpose and not general mathematical or logical computation.

OperatorDescription
:Colon operator. It creates the series of numbers in sequence for a vector.
%in%It is used to identify wheter an element belongs to a vector.
%*%It is used to multiply a matrix with its transpose.

Example:

p <- 1:7
print(p) 
#output
[1] 1 2 3 4 5 6 7
k1 <- 3
k2 <- 10
m <- 1:8
print(k1 %in% m) 
print(k2 %in% m) 
#output
[1] TRUE
[1] FALSE
m = matrix( c(2,3,7,1,12,5), nrow = 2,ncol = 3,byrow = TRUE)
s = m %*% t(m)
print(s)
#output
     [,1] [,2]
[1,]   62   73
[2,]   73  170

Congratulations

You have successfully done working with operators in R.