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"
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]) # wrongYou 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)
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 >
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
"juli g. pausas" <juli at ceam.es> writes:> 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....mean(get(varn[i])). However, the preferred method might be sapply(list(aaa,bbb,ccc),var) or sapply(data.frame(aaa,bbb,ccc),var) (which of course only works when the vectors have the same length, and reminds me that one of the bricks in my road to Hell is to write an nlist() which supplies element names like data.frame does...) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907