[R] Indexing Question

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Oct 9 21:10:19 CEST 2003


"Nathan Cooper" <nathanwands at hotmail.com> writes:

> I have an indexing question. I have a data set that looks like this:
> 
> >b
> [[1]]
> [1] 22 23 24 25 26
> 
> [[2]]
> [1] 6 7 8 9 NA
> 
> etc. from [[1]] to [[1000]]
> 
> Then I need to use the sample function to take two samples from b[[1]]
> to b[[1000]] each separately. I thought something like
> "sample(na.omit(b[[1:1000]]),2,replace=TRUE)" would work but it
> doesn't. Is there a way to index this properly?? Thanks,

Not really.

I'd try

lapply(lapply(b,na.omit),sample,2,replace=TRUE)

or

lapply(b,function(x)sample(na.omit(x),2,replace=TRUE))

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




More information about the R-help mailing list