[R] extraction of vector elements to new list

Patrick Zimmermann brassnotdead at googlemail.com
Mon Jul 23 15:59:46 CEST 2007


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



More information about the R-help mailing list