carol white
2007-Nov-15 18:05 UTC
[R] how to extract the elements of a list of vectors in a fixed position?
Hi, How is it possible to extract athe elements of a list of vectors in a fixed position? suppose that I have a list of 2-element vectors, how can I extract the 2nd element of all vectors in the list? Can it be done with indexing and not by element name? Thanks carol So in this example, I want to extract 2 and 4 v = list (c(1,2), c(3, 4))> v[[1]] [1] 1 2 [[2]] [1] 3 4 --------------------------------- Never miss a thing. Make Yahoo your homepage. [[alternative HTML version deleted]]
Dimitris Rizopoulos
2007-Nov-15 19:53 UTC
[R] how to extract the elements of a list of vectors in a fixed position?
try this: v <- list(c(1,2), c(3,4), 5, c(-3:3)) sapply(v, "[", i = 2) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting carol white <wht_crl at yahoo.com>:> Hi, > How is it possible to extract athe elements of a list of vectors in > a fixed position? suppose that I have a list of 2-element vectors, > how can I extract the 2nd element of all vectors in the list? Can it > be done with indexing and not by element name? > > Thanks > > carol > > So in this example, I want to extract 2 and 4 > v = list (c(1,2), c(3, 4)) > >> v > [[1]] > [1] 1 2 > > [[2]] > [1] 3 4 > > > > > --------------------------------- > Never miss a thing. Make Yahoo your homepage. > [[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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Rolf Turner
2007-Nov-15 19:58 UTC
[R] how to extract the elements of a list of vectors in a fixed position?
On 16/11/2007, at 7:05 AM, carol white wrote:> Hi, > How is it possible to extract athe elements of a list of vectors in > a fixed position? suppose that I have a list of 2-element vectors, > how can I extract the 2nd element of all vectors in the list? Can > it be done with indexing and not by element name? > > Thanks > > carol > > So in this example, I want to extract 2 and 4 > v = list (c(1,2), c(3, 4)) > >> v > [[1]] > [1] 1 2 > > [[2]] > [1] 3 4 >lapply(v,function(x){x[2]}) or unlist(lapply(v,function(x){x[2]})) or (I can't resist) unlist(lapply(v,function(x,i){x[i]},i=2)) # For more flexibility. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}