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