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

Xiaohua Dai ecoinformatics at gmail.com
Fri Jun 30 02:00:06 CEST 2006


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 at 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 at 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 at 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
> >



More information about the R-help mailing list