Dynamic variable names in R

Assigning dynamic variable names

Sometimes you want to use dynamic variable names, these are variable names that change (dynamically) per iteration.

In the basic example below each value in the vector my.vec is iteratively re-named.

Example

my.vec<-c(1:20)

for (i in my.vec) {
  
  # create the dynamic variable name
  dynamic.name <- paste0("variable.", i)
  
  # assign dynamic name to the variable
  assign(dynamic.name, i)
  
}

First a dynamic name is defined, using the paste0() function to convert the current variable name to a character and combine the string “variable.” to the variable name.

Then the dynamic name is assigned to the variable using the assign() function.

The final result in this example is that the variable named “1″ becomes “variable.1”, “2″ becomes “variable.2” and so on.

More examples

Generally I have used this method when I want to store the name of a data frame as a character object to rename variables that are created in a function.

To do this we need to make use of the deparse(), substitute() and paste0() functions to transform our data frame object into a character object.

Here we have two data frames, imagine that they correspond to different analytical models: model1 and model2.

Our aim is to keep the string of the model name accessible as a character object within the function.

This is so that we can use the character object to assign new variable names later on.

model1 <- data.frame(var1=rnorm(10), var2=rnorm(10))
model2 <- data.frame(var1=rnorm(10), var2=rnorm(10))


example.func<-function(my.df){
  
  # save input variable name
  input.variable.name <- deparse(substitute(my.df))
  
  # make dynamic name
  dynamic.name <- paste0(input.variable.name)
  
  # do something with your data (calculate the exponential)
  new.df<-exp(my.df)
  
  # assign dynamic name to new.df
  assign(paste0("exp.", paste0(dynamic.name)), new.df, envir = .GlobalEnv)
  
}

example.func(model1)
example.func(model2)

When we call the example.func function multiple times we can assign names relating to each model within the function.

In the assign function we can add additional text using the paste() function. In this example I have put “exp” to illustrate the calculation of the exponential values.

The result of this function is the new data frame named after the process and the model e.g for model1 our output variable will be called exp.model1.

Using the comments function

Recently I came a cross an interesting use of the comments() function in R.

The comments function allows you to retrieve or assign notes associated with an object. For example you can use this to attach information to a data frame.

Another way you can use this function, as suggested in this post on stack overflow, is to use it to carry the variable name within a loop, For example:

model1 <- data.frame(var1=rnorm(10), var2=rnorm(10))
model2 <- data.frame(var1=rnorm(10), var2=rnorm(10))

# add comment of variable name to the data frames
comment(model1)<-"model1"
comment(model2)<-"model2"


example.func2<-function(my.df){
  
  # define input.variable.name as the comment
  input.variable.name<-comment(my.df)
  
  # do something with your data (exponential)
  new.df<-exp(my.df)
  
  # assign dynamic name to new.df
  assign(paste0("exp.", paste(input.variable.name)), new.df, envir = .GlobalEnv)
  
}

example.func2(model1)
example.func2(model2)

The result of this function example.func2 is the same as example.func where this time the variable name is defined via the comments() function.