Sometimes in programming, it is helpful to have alternative ways of doing things. Not only can this provide for some variety in your code, but sometimes it can also make it more readable. It turns out that the R programming language has three separate ways to assign values to a variable. Knowing all three will help you to be able to read any R program that you find.
Description
R programming has three ways of assigning values to a variable. They are =, <-, and the assign function. The assign function has the basic format of assign(“variable”, value) where “variable” is the name of the variable receiving the values, note that it needs to be in quotes, and “value” is the value being assigned to that variable. While under most circumstances they work the same, they do have differences. The assign function has the advantage that it will show up in a text search of the program when you are looking for assign, while the two operators will not show up in such a search. In practice it differs little from the assign operators of = and <-, but it does resemble similar functions from other languages.
Explanation
The assign function has two required arguments, two are optional arguments you are likely to use, and two arguments that exist to maintain compatibility. The two required arguments are the variable and the value being assigned to it. The two optional arguments are the “pos” which indicates the environment the object is assigned to and ‘inherits” is a Boolean that indicates whether or not to restrict the object to the current environment. The two compatibility arguments are “envir” which selects the environment of the object and “immediate” which is unused and exists only for compatibility reasons. The assign function takes the variable name and assigns the indicated value to it within the specified environmental conditions. These arguments give the assign function more flexibility than the assign operators, making it a particularly useful function under the right conditions.
Examples
Here we have four code examples that show different situations where the assign function is being used. It needs to be noted that this function can be used with any values that the assign operators can use.
> assign(“x”, 17)
> x
[1] 17
In this example, we illustrate the most basic use of the assign function. In this case, we are simply assigning a value to a single variable.
> assign(“x”, c(“a”, “b”, “c”, “d”, “e”, “f”, “g”))
> x
[1] “a” “b” “c” “d” “e” “f” “g”
In this example, we illustrate the creation of a vector using the combine function.
> assign(“x”, c(“a”, “b”, “c”, “d”, “e”, “f”, “g”))
> assign(“y”, 1:7)
> assign(“df”, data.frame(x,y))
> df
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
7 g 7
In this example, we illustrate the creation of a data frame using the data frame function in its simplest form. It is easy to see how using this function to create more complex data frames could become problematic and rather cumbersome.
> for(i in 1:5) {
+ assign(paste0(“x_”, i), i)
+ }
> x_1
[1] 1
> x_2
[1] 2
> x_3
[1] 3
> x_4
[1] 4
> x_5
[1] 5
In this example, we illustrate how to use the assign function along with the paste0 function to create multiple variables with different values.
Application
The main application of the assign function is to assign values to variables. However, this function has additional arguments that give it increased environmental flexibility, allowing you to control whether or not the object that you are creating is restricted to a particular environment. This makes the assign function more powerful in some ways than the assign operators. As a result, an important application of this function is to be able to assign values to variables under circumstances where the program needs to decide on the fly the environmental limitations of the object being created. If you need this flexibility, that is the time to use the assign function. The other situation where this function comes in handy would be if you were creating code that is designed to be as searchable as possible because the word “assign” is a search term that someone is likely to use.
The assign function may at first glance seem redundant, but its additional arguments give it added flexibility over the assign operators. As a result, this function can actually be a useful tool. This flexibility makes it a powerful tool that you will probably find more useful as you get more experience with it.