Re: [R] How to use a for loop to generate two sequences

From: Xiaohua Dai <ecoinformatics_at_gmail.com>
Date: Fri 30 Jun 2006 - 10:00:06 EST

I followed your method but there was an error on FUN get:

> nm <- paste("test", 1:1000, sep = "")
> lapply(nm, get)
Error in get(x, envir, mode, inherits) : variable "test1" was not found
> L <- sapply(paste("test", 1:1000, sep = ""), get, simplify = FALSE)
Error in get(x, envir, mode, inherits) : variable "test1" was not found

But lapply(nm, as.name) worked well. Are there any problem with my R env?

> traceback()

2: FUN(X[[1]], ...)
1: lapply(nm, get)

> sessionInfo()

Version 2.3.1 (2006-06-01)
i386-pc-mingw32

attached base packages:
[1] "methods" "stats" "graphics" "grDevices" "utils" "datasets" [7] "base"

> win.version()

[1] "Windows XP Professional (build 2600) Service Pack 2.0"

Thanks
Xiaohua

On 6/30/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> A vector of character strings:
>
> nm <- paste("test", 1:1000, sep = "")
>
> A list whose components are the objects: test1, test2, ...
>
> lapply(nm, get)
>
> or if you do it this way the list will have 'test1', 'test2', ... as the
> component names:
>
> L <- sapply(paste("test", 1:1000, sep = ""), get, simplify = FALSE)
>
> and then
>
> names(L)
>
> gives the vector of names.
>
>
> Also note that
>
> apropos("^test[0-9]+")
>
> will give a vector of object names corresponding to objects
> in the current workspace whose names
> are "test" followed by a number.
>
> On 6/29/06, Xiaohua Dai <ecoinformatics@gmail.com> wrote:
> > Hi R users,
> >
> > Hope the question is not too simple:
> >
> > How to use a for loop to generate two lists as below:
> >
> > testlist <- list(test1, test2, ..., test1000)
> > stringlist <- list("test1","test2",...,"test1000")
> >
> > Thanks
> > Xiaohua
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 30 10:09:13 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 30 Jun 2006 - 12:13:44 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.