Assignment Operators in R

In this tutorial, you will learn about the assignment operators in R programming language and how to use them.

Assignment Operators in R

An assignment operator is used to assign values or data to a named object to store the data.

The basic assignment operator in R is <-, which is known as leftward assignment operator. In R programming it is recommended to use leftward (<-) assignment which is the combination of two characters less than (<) and minus (-).

The two other assignment operators in R are the rightward assignment operator (->) and the equal to assignment operator (=). The equal (=) operator is not recommended for assignment in R. It is used in argument assignment in R functions.

The following table summarizes assignment operators in R:

Operator Symbol Assignment Operation Example
<- Leftward assignment x <- 8
-> Rightward assignment 9 -> y
= Leftward assignment x = 8
<<- Leftward assignment x <<- 8
->> Rightward assignment 9 ->> y

The assignment operator <- or -> assigns the value to the object and store in the local (current) environment, whereas <<- or ->> assigns the values to the object and store in the global environment.

Examples of Assignment Operators

Following code illustrate the use of leftward, rightward and equal assignment operators.

Leftward Assignment

To assign the value 5 to the variable (object) x using leftward (<-) assignment operator, use the below R code.

x <- 5
x
[1] 5

Above code create a local object with name x in R environment and store the value 5 in it.

Note that, while using leftward assignment (<-), the object name in which you want to store the value needs to be at the left.

Rightward Assignment

To assign the value 10 to the object y using rightward (->) assignment operator, use the below R code.

10 -> y
y
[1] 10

Above code create a local object with name y in R environment and store the value 10 in it.

Note that, while using rightward assignment (->), the object name in which you want to store the value needs to be at the right.

Assignment with = sign

To assign the value 12 to the object z using equal to (=) assignment operator, use the below R code.

z = 12
z
[1] 12

Above code create a local object with name z in R environment and store the value 12 in it.

Note that, while using equal to assignment operator (=), the object name in which you want to store the value must be at the left side.

Note that 12 = z display an error message.

Endnote

In this tutorial you learned about different assignment operators in R.

To learn more about other operators in R, please refer to the following tutorials:

Arithmetic operators
Relational operators
Logical operators in R
Miscellaneous operators in R
Precendence of Operators in R
Operators in R

Hopefully you enjoyed learning this tutorial on assignment operators in R.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment