R - left assignment operator example

Left Assignment operator assigns value of right hand side expression to left hand side operand. R has three such kind of operators (=, <- and <<-).

Left assignment operator (=)

It assigns value of right hand side expression to left hand side operand.

x1 = 10
x2 = c(10, 20, 30)
x3 = matrix(c(10, 20, 30, 40, 50, 60), nrow=2)

#printing all variables
print(x1)
print(x2)

cat("\n")
print(x3)

The output of the above code will be:

 10
 10 20 30

[,1] [,2] [,3]
[1,]   10   30   50
[2,]   20   40   60

Left assignment operator (<-)

It assigns value of right hand side expression to left hand side operand. It is same as (=) except it can be used anywhere, whereas the operator (=) is only allowed at the top level.

x1 <- 10
x2 <- c(10, 20, 30)
x3 <- matrix(c(10, 20, 30, 40, 50, 60), nrow=2)

#printing all variables
print(x1)
print(x2)

cat("\n")
print(x3)

The output of the above code will be:

 10
 10 20 30

[,1] [,2] [,3]
[1,]   10   30   50
[2,]   20   40   60

Left assignment operator (<<-)

It assigns value of right hand side expression to left hand side operand. It has same functionality as (<-) but as global assignment operator.

x1 <<- 10
x2 <<- c(10, 20, 30)
x3 <<- matrix(c(10, 20, 30, 40, 50, 60), nrow=2)

#printing all variables
print(x1)
print(x2)

cat("\n")
print(x3)

The output of the above code will be:

 10
 10 20 30

[,1] [,2] [,3]
[1,]   10   30   50
[2,]   20   40   60

❮ R - Operators

5