# 1st el of a list of vectors

5 messages
Open this post in threaded view
|

## 1st el of a list of vectors

 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 1st el of a list of vectors

 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 <[hidden email]> 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]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 1st el of a list of vectors

 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):    > library(IRanges)    > eltlens <- elementLengths(l)    > unlist(l, use.names=FALSE)[cumsum(eltlens) - eltlens + 1L]    [1] 1 3 7 Only worth if the length of your list is > 100000 though... Cheers, H. PS: See http://bioconductor.org/packages/release/bioc/html/IRanges.htmlfor how to install the IRanges package. > > On Mon, Jul 21, 2014 at 3:55 PM, carol white <[hidden email]> 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]] >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. > -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: [hidden email] Phone:  (206) 667-5791 Fax:    (206) 667-1319 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.