[R] Loop on vector name
Greg.Snow at imail.org
Thu Sep 18 00:14:22 CEST 2008
Others have answered the question that you asked (it is also a variation of Faq 7.21), but here is an answer to the question that you should have asked:
When working with datasets like this, it is better to create a list rather than separate objects with names like dat1, dat2, etc.
> mydat <- list(dat1=rnorm(10), dat2=rnorm(25), dat3=runif(100))
Now your loop can be:
> for(i in 1:3) cat( sd( mydat[[i]] ) )
Or even simpler:
> sapply( mydat, sd )
And when you are through with the data, you only need to delete one object, or copy one object, or save one object, etc.
Hope this helps,
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Megh Dal
> Sent: Wednesday, September 17, 2008 5:51 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Loop on vector name
> [My previous message rejected, therefore I am sending same one with
> some modification]
> I have 3 vectors with object name : dat1, dat2, dat3
> Now I want to create a loop, like :
> for (i in 1:3)
> How I can do this in R?
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help