[R] Selecting first 7 elements

Erik Iverson eriki at ccbr.umn.edu
Sun May 23 09:08:33 CEST 2010


Kang Min wrote:
> Hi,
> 
> I have a list of 100, each list has 20 elements, and I would like to
> select the first 7 elements in each list.
> Let's take the alphabet as an example.
> 
> x <- lapply(1:100, function(i) sample(LETTERS))
> 
> I tried x[[1:7]], but it doesn't work. Can anyone enlighten me on how
> to do such selections?

"[" is a function, and you want to use it on each element of the list, so...

lapply(x, "[", c(1:7))



More information about the R-help mailing list