[R] Indexing lists

Gabor Grothendieck ggrothendieck at myway.com
Thu Sep 16 14:08:05 CEST 2004


Perez Martin, Agustin <agustin.perez <at> umh.es> writes:


: I have a list with 500 elements, in each other there are data.frames and I
: want to take the first row and the first column of each elements of my list
: since the first to the 500-th.


Here are some variations depending on what you want.  Also try
all these with lapply replaced by sapply.

# test data
data(iris)
iris <- head(iris)
L <- list(iris, iris)

# various possibilities 
lapply(L, "[", 1, 1) # column 1, row 1
lapply(L, "[[", 1) # column 1 
lapply(L, "[", 1)  # column 1 as data frame
lapply(L, function(x) x[1,]) # row 1
lapply(L, function(x) list(x[1,],x[,1]))  # 2 el list with row 1 and column 1




More information about the R-help mailing list