Dear R-community, I have got a list of vectors and would like to extract the first two elements of each vector to a new list. My list is of the style: my.list = list(c("a", "b", "c"), c("d", "e"), c("f", "g", "h", "i"), ...) #I want: new.list = list(c("a", "b"), c("d", "e"), c("f", "g"), ...) # As my.list[[3]][1:2] # is [1] "f" "g" # I thought my.list[[1:3]][1:2] # would be # [[1]] # [1] "a" "b" # [[2]] # [1] "d" "d" # [[3]] # [1] "f" "g" # but is: 'Error: recursive indexing failed at level 2' I think it should be easy, but none of my tried combinations of '[' and 'c(' worked. Who can help? Patrick
try this: new.list <- lapply(my.list, "[", i = 1:2) new.list 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 ----- Original Message ----- From: "Patrick Zimmermann" <brassnotdead at googlemail.com> To: <R-help at stat.math.ethz.ch> Sent: Monday, July 23, 2007 3:59 PM Subject: [R] extraction of vector elements to new list> Dear R-community, > > I have got a list of vectors and would like to extract the first two > elements of each vector to a new list. > > My list is of the style: > > my.list = list(c("a", "b", "c"), c("d", "e"), c("f", "g", "h", "i"), > ...) > > #I want: > > new.list = list(c("a", "b"), c("d", "e"), c("f", "g"), ...) > > # As > > my.list[[3]][1:2] > > # is [1] "f" "g" > > # I thought > > my.list[[1:3]][1:2] > > # would be > > # [[1]] > # [1] "a" "b" > > # [[2]] > # [1] "d" "d" > > # [[3]] > # [1] "f" "g" > > # but is: 'Error: recursive indexing failed at level 2' > > > I think it should be easy, but none of my tried combinations of '[' > and 'c(' worked. > Who can help? > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch 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
Hi, try lapply(my.list, function(x)head(x, n=2)) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 23/07/07, Patrick Zimmermann <brassnotdead@googlemail.com> wrote:> > Dear R-community, > > I have got a list of vectors and would like to extract the first two > elements of each vector to a new list. > > My list is of the style: > > my.list = list(c("a", "b", "c"), c("d", "e"), c("f", "g", "h", "i"), ...) > > #I want: > > new.list = list(c("a", "b"), c("d", "e"), c("f", "g"), ...) > > # As > > my.list[[3]][1:2] > > # is [1] "f" "g" > > # I thought > > my.list[[1:3]][1:2] > > # would be > > # [[1]] > # [1] "a" "b" > > # [[2]] > # [1] "d" "d" > > # [[3]] > # [1] "f" "g" > > # but is: 'Error: recursive indexing failed at level 2' > > > I think it should be easy, but none of my tried combinations of '[' > and 'c(' worked. > Who can help? > > Patrick > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
On 7/23/07, Dimitris Rizopoulos <dimitris.rizopoulos at med.kuleuven.be> wrote:> try this: > > new.list <- lapply(my.list, "[", i = 1:2) > new.listYou could use the lapply above or lapply(my.list, head, 2)