An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140721/e7c41898/attachment.pl>
1st el of a list of vectors
5 messages · carol white, Richard M. Heiberger, Hervé Pagès +2 more
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_crl at 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 at 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.
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.html for how to install the IRanges package.
On Mon, Jul 21, 2014 at 3:55 PM, carol white <wht_crl at 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 at 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 at 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.
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: hpages at fhcrc.org Phone: (206) 667-5791 Fax: (206) 667-1319
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 <rmh at 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_crl at 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 at 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 at 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.
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.81 0.00 0.81 ## But
system.time(r3<- unlist(lapply(l1,`[`,1)))
user system elapsed 0.64 0.00 0.66 ## and
system.time(r4<- vapply(l1,`[`,1,1))
user system elapsed 0.60 0.00 0.61 sapply() takes a bit of extra time to get the return into the right data structure. You tell vapply the return type and lapply always returns a list. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll
On Tue, Jul 22, 2014 at 1:14 AM, arun <smartpink111 at yahoo.com> wrote:
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 <rmh at 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_crl at 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 at 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 at 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 at 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.