Using variables names in loops

Hi there,

I am kind of you to R, I used to work on Stata, so it is kind to make the change.

I wish to run through variables using their names.

a1 = c(1,2,3,4,5,6)
a2 = c(1,2,3,4,5,6)
df = data.frame(a1,a2)
for (i in 1:2) {
  variable = paste0("a", i)
  mean = mean(as.name(variable))
  print(as.name(mean))
}

This actually doesn't work, since it is not taking the variable values but the variable as a name. On top of that I wish to create numerous mean variables using the number in the loop (for instance mean1 and mean2 in the present case).
For those of you which are familiar with Stata, here is what I'm trying to do

forvalues x = 1/2 {
gen meanx' = mean(ax')
}

Thank you so much in advance!

Bests,

The below code works:

a1 = c(1,2,3,4,5,6)
a2 = c(1,2,3,4,5,6)
df = data.frame(a1,a2)
for (i in 1:2) {
  variable = paste0("a", i)
  mean = mean(eval(parse(text = variable)))
  print(mean)
}

However, there are other ways to achieve similar results.

I notice you've created a dataframe; this would also do what you want:

for (i in 1:2) {
  variable = paste0("a", i)
  mean = mean(df[[variable]])
  print(mean)
}

Or you could avoid loops altogether and use {dplyr}, which will retain your dataframe structure:

dplyr::summarise(df, dplyr::across(contains("a"), ~mean(.x)))
   a1  a2
1 3.5 3.5

Thank you very very much! This is exactly what I needed.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.