# Guide to Using the assign() Function in R – With Examples

The R programming language is among the single most popular options for statistical computing and data science. One of the reasons for this popularity stems from the fact that R is designed around data processing. All programming languages work with data processing to some extent. But with R, that functionality is at the heart of every design decision. However, this can also make R a little unconventional when compared to other popular programming languages. And this is especially apparent when we look at the proper use and functionality of R’s assign() function. But you’ll soon see why assign is important, how it allows for functionality not found in other languages, and how to get the most out of it.

## The Basics of R’s Assign

Assign is among the more unassuming functions in R’s toolkit. At first glance, it seems like one of those cruft functions that often persist in a language long after its usefulness is over. This is because by default the assign function is just a slightly more verbose way of enacting a standard variable assignment. Assign lives up to its name by quite literally just taking a value and assigning it to a string. But assign’s true potential comes from the fact that it leverages functionality typically limited by standard assignment operators. This gives us the power to essentially override standard variable assignments in a number of creative ways.

## A Simple Example of Apply in Action

We can begin by looking at the most basic usage scenarios for assign. Try running the following code.

assign(“ourVar”, 999)
ourVar2 <- 999

print(ourVar)
print(ourVar2)
print(typeof(ourVar))
print(typeof(ourVar2))

This code demonstrates how similar assign is to a standard variable assignment. We begin by calling assign with two arguments. These are a string consisting of “ourVar” and the number 999. Next, we use the standard R syntax to assign 999 to a second variable called ourVar2. We proceed to print out some information about each variable so that we can compare them against each other. You’ll see that both variables are seen by R as a double whose value is 999.

In other words, the assign function and a standard variable assignment have produced identical results. The main difference seems to be that assign requires slightly more work to set up than a standard assignment operation. You’d be justified in wondering what the function’s point is. But, as mentioned earlier, we can use extend to leverage a lot of additional power from R’s underlying functionality that’s normally closed off from end users.

## More Advanced Usage Scenarios for Assign

We can most easily understand apply’s real power by seeing it in action within a more complex context. Take a look at the following example.

ourData <- data.frame(
first = c (“alpha”,”beta”,”epsilon”,”eta”),
second = c(“A”,”B”,”E”,”H”)
)

nu <- “N”

for(x in 1:ncol(ourDataFrame)) {
print(ourData[x,1])
print(ourData[x,2])
assign(paste0(ourData[x,1]), ourData[x,2])
}

print(typeof(alpha))
print(alpha)

print(typeof(nu))
print(nu)

We begin by defining a new data frame as ourData. This contains two separate rows with values corresponding to elements of the greek alphabet. The data frame definition is followed by a single definition of a variable, nu, as “N”. We move onward to create a for loop that iterates over the columns in ourData. With each loop the code prints out the current value of the row and column in ourData. We also call the assign function using those same values.

When the loop is finished we print out the type and contents of two variables. These consist of an alpha variable and nu variable. The nu variable is exactly as we’d expect given the explicit declaration at the start of the code. But alpha will probably come as more of a surprise. Because there is no alpha variable declaration in our code. And yet the R interpreter doesn’t throw an error message when we attempt to work with it. We even find a legitimate value stored within the alpha variable. How did that happen? The answer comes down to the assign function we used within the loop.

Remember how we printed out the corresponding items in each of ourData’s columns? Those were also passed to assign. We gave the names in the first row to assign as its initial argument. And the value in the second row was passed as the second argument. For the first pass in the loop we essentially called assign as seen in the line below.

assign(“alpha”, “A”)

And this assignment remains consistent for every other item within ourData. And at this point, you’ve seen the true power of R’s assign function. We normally can’t run an assignment operation with entirely programmatically defined elements. But assign essentially lets us bypass that limitation to directly access assignment logic. And because we’re able to manipulate it any way we want we can integrate it into loops or any other logic structure.

Another interesting component of the assign function stems from the fact that it only needs an input string to create variable names. Likewise, any standard value can be passed as the second argument. In the initial example, we simply provided a string and value of 999. But in this example, we’re technically not working with a string for assign’s initial argument. We’re instead working with a data frame. And it’s that data frame that contains a string. This all comes down to R’s inherent flexibility.

R’s focus on data manipulation means that it’s generally trivial to convert different data types. And we see this with the way we call assign in the second example. We’re not just passing the contents of the data frame as assign’s first argument. We use print0 to ensure that anything passed to assign is in string format. So if we had a numeric value instead of the alpha string then assign would still be able to accept it as a valid variable name. Of course in a larger project, we would generally want to avoid using numbers as a variable name. But this can be quite useful for smaller projects where we simply need to import and manipulate a collection of data.

Scroll to top