[R] looping through numbered variables

Daniel Caro dcarov at gmail.com
Mon Aug 27 16:11:31 CEST 2012


Hello,

This is a beginner question. I am trying to loop through numbered
variables with "apply" to calculate weighted means. My data is "data",
the variables are "var1" to "var100", the weight is "weight". The
command works using

sapply(paste('data$var', 1:100, sep=''), function(x)
weighted.mean(eval(parse(text=x)), data$weight))

but is there a way to avoid eval(parse())?

Thank you,
Daniel



More information about the R-help mailing list