Assignment Operators in R (3 Examples) | Comparing = vs. <- vs. <<-
On this page you’ll learn how to apply the different assignment operators in the R programming language.
The content of the article is structured as follows:
Let’s dive right into the exemplifying R syntax!
Example 1: Why You Should Use <- Instead of = in R
Generally speaking, there is a preference in the R programming community to use an arrow (i.e. <-) instead of an equal sign (i.e. =) for assignment.
In my opinion, it makes a lot of sense to stick to this convention to produce scripts that are easy to read for other R programmers.
However, you should also take care about the spacing when assigning in R. False spacing can even lead to error messages.
For instance, the following R code checks whether x is smaller than minus five due to the false blank between < and -:
my_object1 < -5 # Spaces matter # Error: object 'my_object1' not found
A properly working assignment could look as follows:
my_object2<-5 # Hard to read my_object2 # Print output # 5
However, this code is hard to read, since the missing space makes it difficult to differentiate between the different symbols and numbers.
In my opinion, the best way to assign in R is to put a blank before and after the assignment arrow:
my_object3 <- 5 # Easy to read my_object3 # Print output # 5
As mentioned before, the difference between <- and = is mainly due to programming style. However, the following R code using an equal sign would also work:
my_object4 = 5 # Unconventional code my_object4 # Print output # 5
In the following example, I’ll show a situation where <- and = do not lead to the same result. So keep on reading!
Example 2: When <- is Really Different Compared to =
In this Example, I’ll illustrate some substantial differences between assignment arrows and equal signs.
Let’s assume that we want to compute the mean of a vector ranging from 1 to 5. Then, we could use the following R code:
mean(x = 1:5) # Does NOT save values in x # 3
However, if we want to have a look at the vector x that we have used within the mean function, we get an error message:
x
# Error: object 'x' not found
Let’s compare this to exactly the same R code but with assignment arrow instead of an equal sign:
mean(x <- 1:5) # Does save values in x # 3
The output of the mean function is the same. However, the assignment arrow also stored the values in a new data object x:
x # Print x # 1 2 3 4 5
This example shows a meaningful difference between = and <-. While the equal sign doesn’t store the used values outside of a function, the assignment arrow saves them in a new data object that can be used outside the function.
Example 3: The Difference Between <- and <<-
So far, we have only compared <- and =. However, there is another assignment method we have to discuss: The double assignment arrow <<- (also called scoping assignment).
The following code illustrates the difference between <- and <<- in R. This difference mainly gets visible when applying user-defined functions.
Let’s manually create a function that contains a single assignment arrow:
my_fun1 <- function() { # User defined function with <- x_fun1 <- 5 }
Now, let’s apply this function in R:
my_fun1() # Execute function with <-
The data object x_fun1, to which we have assigned the value 5 within the function, does not exist:
x_fun1 # Return output of function with <- # Error: object 'x_fun1' not found
Let’s do the same with a double assignment arrow:
my_fun2 <- function() { # User defined function with <<- x_fun2 <<- 5 }
Let’s apply the function:
my_fun2() # Execute function with <<-
And now let’s return the data object x_fun2:
x_fun2 # Return output of function with <<- # 5
As you can see based on the previous output of the RStudio console, the assignment via <<- saved the data object in the global environment outside of the user-defined function.
Video & Further Resources
I have recently released a video on my YouTube channel, which explains the R syntax of this tutorial. You can find the video below:
The YouTube video will be added soon.
In addition to the video, I can recommend to have a look at the other articles on this website.
In summary: You learned on this page how to use assignment operators in the R programming language. If you have further questions, please let me know in the comments.
assignment-operators-in-r
How to use different assignment operators in R – 3 R programming examples – R programming language tutorial – Actionable R programming syntax in RStudio
Statistics Globe Newsletter