Name Variables in for-Loop Dynamically in R (2 Examples)

 

In this tutorial, I’ll illustrate how to name variables dynamically in a for-loop in R programming.

Table of contents:

If you want to know more about these contents, keep reading!

 

Introduction of Exemplifying Data

I will use the following list object as basement for the examples of this R programming tutorial:

my_list <- list(1:5,                             # Create list in R
                "AAA",
                9:5)
my_list                                          # Print list to console
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "AAA"
# 
# [[3]]
# [1] 9 8 7 6 5

Our example list consists of three list elements. None of these list elements has a name.

 

Example 1: Basic Application of assign Function

Let’s assume that we want to create a new variable containing the values of the first list element. This variable should be named variable_1. Then, we can apply the assign function as shown below:

assign("variable_1", my_list[[1]])               # Apply assign function
variable_1
# 1 2 3 4 5

An advantage of the assign function is that we can create new variables based on a character string. You’ll see this advantage in the next example in action!

 

Example 2: Applying assign Function in for-Loop

The following R codes is again using the assign function, but this time within a for-loop. Note that we are using the indicator i as part of the new variable names by concatenating i with the prefix variable_ using the paste0 function:

for(i in 1:length(my_list)) {                    # assign function within loop
  assign(paste0("variable_", i), my_list[[i]])
}

Let’s have a look at the output:

variable_1                                       # Print variables
# 1 2 3 4 5
variable_2
# "AAA"
variable_3
# 9 8 7 6 5

As you can see based on the previously shown output of the RStudio console, we created three new variables on the fly.

Note that we could apply the same principle to any other kind of for-loop or also to while-loops and manually defined functions.

 

Video, Further Resources & Summary

Do you need more explanations on the R programming codes of this article? Then you might have a look at the following video of my YouTube channel. I show the R programming code of this tutorial in the video:

 

 

In addition, I can recommend to read the related tutorials which I have published on this website:

 

Summary: At this point you should know how to create vector variables dynamically in the R programming language. Let me know in the comments section, if you have any further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

  • thanks! I am trying to use this in a for loop and would like to find a way to call this “dynamic” variable by a “universal” name. Could you help me with this?

    for(Var in unique(df$Group)) {
    assign(paste(“T1_”, Var, sep = “”), median(filter(df, Group == Var)$T1))
    }
    I want to call the “T1_Var”

    Reply
    • Hey,

      It’s difficult to tell without seeing your data, but does the following code work for you?

      for(i in 1:length(unique(df$Group))) {
        assign(paste("T1_", unique(df$Group)[i], sep = ""), median(filter(df, Group == unique(df$Group)[i])$T1))
      }

      Regards

      Joachim

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top