# [R] var[i]

Ko-Kang Kevin Wang kwan022 at stat.auckland.ac.nz
Thu May 1 19:32:23 CEST 2003

```Hi,

If I understand you correctly, then:

On Thu, 1 May 2003, juli g. pausas wrote:

> 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")

Take a look at your varn here.  You'll notice :
> varn
[1] "aaa" "bbb" "ccc"

> m <- rep(NA, 3)
>
> for (i in 1:length(varn)) m[i] <- mean(varn[i])  # wrong

You can probably do this as a list:
aaa <- 1:10
bbb <- aaa*2
ccc <- aaa+bbb

varn <- list(aaa, bbb, ccc)
m <- rep(NA, 3)

for (i in 1:length(varn)) m[i] <- mean(varn[[i]])  # wrong

m

--
Cheers,

Kevin

------------------------------------------------------------------------------
/* Time is the greatest teacher, unfortunately it kills its students */

--
Ko-Kang Kevin Wang
Master of Science (MSc) Student
SLC Tutor and Lab Demonstrator
Department of Statistics
University of Auckland
New Zealand
Homepage: http://www.stat.auckland.ac.nz/~kwan022
Ph: 373-7599
x88475 (City)
x88480 (Tamaki)

```