[R] var[i]

Liaw, Andy andy_liaw at merck.com
Thu May 1 19:32:28 CEST 2003


Here's an example:

> x1 <- 1:3
> x2 <- 4:6
> x3 <- 7:9
> xn <- paste("x", 1:3, sep="")
> m <- numeric(3)
> for(i in 1:3) m[i] <- mean(get(xn[i]))
> m
[1] 2 5 8

HTH,
Andy

> -----Original Message-----
> From: juli g. pausas [mailto:juli at ceam.es]
> Sent: Thursday, May 01, 2003 1:03 PM
> To: r-help
> Subject: [R] var[i]
> 
> 
> 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
> 
> 
> thanks in advance
> 
> Juli
> 
> 
> 
> --
> "Wars do not solve problems, wars generate even more problems"
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>



More information about the R-help mailing list