[R] extraction of vector elements to new list

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Mon Jul 23 16:34:40 CEST 2007


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



More information about the R-help mailing list