[R] Working with lists

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sat Jul 27 09:02:27 CEST 2002


On Sat, 27 Jul 2002, William Vedder wrote:

> I'm stuck and would appreciate some help.
>
> After writing a function to generate a random sample from vector
> "rand1":
> > f<-function(i) {y<-sample(rand1,296,replace=F)}
>
> and using the function to generate multiple samples from rand1 and store
> the results in list "l":
> > l<-lapply(1:100,f)
>
> I can't get the next step accomplished; I'd like to sort each of the
> generated samples.  I've tried:
> > sort(l)
>
> but get the msg:
> Error in sort(l) : `x' must be atomic

You want to sort the components, not the list (I think) so use lapply
again:

l <- lapply(l, sort)

BTW, a for() loop is also a reasonable solution:

for(i in seq(along=l)) l[[i]] <- sort(l[[i]])

but less readable.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list