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 0.741 identical(r1,r2) #[1] TRUE system.time({ eltlens <- elementLengths(l1) r3 <- unlist(l1, use.names=FALSE)[cumsum(eltlens) - eltlens + 1L] }) # user system elapsed # 0.153 0.000 0.154 A.K. On Tuesday, July 22, 2014 12:11 AM, Richard M. Heiberger <r...@temple.edu> wrote: 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 <wht_...@yahoo.com> 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 should be retrieved > > Thanks > > Carol > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.