[R] var[i]

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Thu May 1 19:33:07 CEST 2003



juli g. pausas wrote:
> Dear all,
> How could I use variables in a loop that their names are in a vector? 
> For example:
> 
> aaa <- 1:10
> bbb <- aaa*2
> ccc <- aaa+bbb
> 
> varn <- c("aaa", "bbb", "ccc")
> m <- rep(NA, 3)
> 
> for (i in 1:length(varn)) m[i] <- mean(varn[i])  # wrong
> 
> 


Use ?get.


for (i in 1:length(varn)) m[i]=mean(get(varn[i]))  # right

sundar



More information about the R-help mailing list