DeaR useRs: 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. Thanks and excuse my bad English. --- [[alternative HTML version deleted]]
Maybe not the best solution, but something like: lapply(mylist,function(x) {return(list(FirstCol=mylist[,1],FirstRow=mylist[1,]))}) In any case, look at ?lapply Sean On Sep 16, 2004, at 7:33 AM, Perez Martin, Agustin wrote:> DeaR useRs: > > 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. > > Thanks and excuse my bad English. > > --- > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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
"Perez Martin, Agustin" <agustin.perez at umh.es> writes:> DeaR useRs: > > 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. > > Thanks and excuse my bad English.Possibly (if I catch your drift...) lapply(yourlist,"[",1,1) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
m <- matrix(1:9, nc=3) a <- list(m, m+10, m+100, m+1000) sapply(a, function(mat) mat[1,1]) [1] 1 11 101 1001 On Thu, 2004-09-16 at 12:33, Perez Martin, Agustin wrote:> DeaR useRs: > > 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. > > Thanks and excuse my bad English. > > --- > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >
Hi, Probably it should be useful to obtain two "results" which you can combine according to what you need. Here a possible solution: a<-b<-data.frame(matrix(runif(30),10,3)) d<-list(a,b) #your list #extract the 1st column. The output is a nrow(a)-by-length(d) matrix sapply(d,function(x)x[,1]) #extract the 1st row. The output is a ncol(a)-by-length(d) matrix sapply(d,function(x)x[1,]) #a ncol(a)-by-length(d) matrix Note if the dimensions of dataframes are different you will get lists. Anyway, if you prefer lists rather than matrices use lapply() best, vito ----- Original Message ----- From: Perez Martin, Agustin <agustin.perez at umh.es> To: <r-help at stat.math.ethz.ch> Sent: Thursday, September 16, 2004 1:33 PM Subject: [R] Indexing lists> DeaR useRs: > > 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 mylist> since the first to the 500-th. > > Thanks and excuse my bad English. > > --- > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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