Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to use the value of an assigned variable in new variable names in R?

Goal: To create a new variable whose name uses the value of a previously assigned variable in R.

Motivation: Naming many variables according to some value related to the associated command’s property (e.g., an alpha value of 0.75 specified for the bar fill on one chart and the point colour on another chart) would make it possible to store many objects with small changes between them on-the-fly without having to change each variable’s name every time the value is changed.

Continuing with the example of the alpha value given to different plots (above), this should allow us to change the variable value from 0.75 to 0.25, re-run the plotting commands, and store these plots in variables representatively named for the new opacity being tested.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Simple Example:

First, a value of 42 is used:

number <- 42

var1_number <- rnorm(number)
var2_number <- runif(number)
var3_number <- log(number)
var4_number <- exp(number)

Then, the value is changed to 17:

number <- 17

var1_number <- rnorm(number)
var2_number <- runif(number)
var3_number <- log(number)
var4_number <- exp(number)

Desired output:

The eight variables should be stored in the R environment as:

Values
var1_42 num [1:42] -0.2298 -0.6823 0.0871 -0.7689 0.1142 . . .
var1_17 num [1:17] 1.241 1.781 1.668 -0.446 -1.668 . . .
var2_42 num [1:42] 0.6926 0.5927 0.7441 0.9804 0.0202 . . .
var2_17 num [1:17] 0.1307 0.3907 0.0599 0.082 0.6091 . . .
var3_42 3.73766961828337
var3_17 2.83321334405622
var4_42 1739274941520500992
var4_17 24154952.7535753

Attempts:

Using the exact example provided above results in the creation of four variables named explicitly as written: var1_number, var2_number, var3_number, var4_number.

Since the variable names are not retrieving an actual value from the defined number variable, the names remain the same and the variables are overwritten when the value stored in number is changed.

Also, using backticks to indicate a variable does NOT work (i.e., var1_`number` <- rnorm(number)) as R interprets them as "unexpected symbol"s.

>Solution :

I suspect lists might be a better choice for keeping these organized.

make_list <- function(x) {
  a <- list()
  a$var1 = rnorm(x)
  a$var2 = runif(x)
  a$var3 = log(x)
  a$var4 = exp(x)
  a
}

var17 <- make_list(17)

This will help keep the results organized, and will also make it much easier to compute on those results.

var17$var1
#[1] -1.30949829  0.51942312 -0.08220472  0.32858289  1.39745159  2.36128781  1.01300924 -1.35826357 -0.98565341  0.80732834
#[11]  0.38359233 -0.37823694  0.51910416  0.81922642  0.62763918  0.70783359  0.02417833

This post shares a useful related case where we might save different results in a list to keep them organized and easy to reference:

tjmahr.com/lists-knitr-secret-weapon

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading