Re: [R] 1st el of a list of vectors

2014-07-22 Thread Bert Gunter
Yes, but note that such comparisons don't necessarily tell you much. Using your l1 on my computer: > system.time(r1 <- sapply(l1, `[`, 1)) user system elapsed 1.2 0.0 1.2 > system.time(r2<- rapply(l1,function(x)x[1])) user system elapsed 0.810.000.81 ## But > syste

Re: [R] 1st el of a list of vectors

2014-07-22 Thread arun
Or rapply(l,function(x) x[1]) #[1] 1 3 7 set.seed(42)  l1 <- replicate(1e6, list(sample(1:5,sample(8),replace=T))) system.time(r1 <- sapply(l1, `[`, 1))  #  user  system elapsed  # 1.324   0.000   1.326 system.time(r2 <- rapply(l1, function(x) x[1])) #   user  system elapsed #  0.736   0.004 

Re: [R] 1st el of a list of vectors

2014-07-21 Thread Hervé Pagès
Hi Carol, On 07/21/2014 09:10 PM, Richard M. Heiberger wrote: l = list(c(1,2), c(3,5,6), c(7)) sapply(l, `[`, 1) Using sapply() works but won't be very efficient if you have a very long list. If you worry about efficiency, you can do the following (using the IRanges package from Bioconductor)

Re: [R] 1st el of a list of vectors

2014-07-21 Thread Richard M. Heiberger
l = list(c(1,2), c(3,5,6), c(7)) sapply(l, `[`, 1) On Mon, Jul 21, 2014 at 3:55 PM, carol white wrote: > Hi, > If we have a list of vectors of different lengths, how is it possible to > retrieve the first element of the vectors of the list? > > > l = list(c(1,2), c(3,5,6), c(7)) > > 1,3,7 shoul

[R] 1st el of a list of vectors

2014-07-21 Thread carol white
Hi, If we have a list of vectors of different lengths, how is it possible to retrieve the first element of the vectors of the list? l = list(c(1,2), c(3,5,6), c(7)) 1,3,7 should be retrieved Thanks Carol [[alternative HTML version deleted]] __