On Feb 12, 2013, at 3:01 PM, Dimitri Liakhovitski wrote: > Arun, do you know why the first line works and the second doesn't? > newlist<-mapply(`[`,mylist,list(1,2,0),SIMPLIFY=FALSE) # it works > neededcolumns<-c(1,2,0) # it does not work > newlist<-mapply(`[`,mylist,list(neededcolumns),SIMPLIFY=FALSE)
> list(c(1,2,3)) [[1]] [1] 1 2 3 > list(1,2,3) [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 -- David. > Dimitri > On Tue, Feb 12, 2013 at 5:08 PM, arun <smartpink...@yahoo.com> wrote: > >> Hi, >> >> >> mapply(`[`,mylist,list(1,2,0),SIMPLIFY=FALSE) >> #[[1]] >> # a >> #1 1 >> #2 2 >> >> #[[2]] >> # b >> #1 5 >> #2 6 >> >> #[[3]] >> #data frame with 0 columns and 2 rows >> >> >> A.K. >> >> ----- Original Message ----- >> From: Dimitri Liakhovitski <dimitri.liakhovit...@gmail.com> >> To: r-help <r-help@r-project.org> >> Cc: >> Sent: Tuesday, February 12, 2013 4:33 PM >> Subject: [R] grabbing from elements of a list without a loop >> >> Hello! >> >> # I have a list with several data frames: >> mylist<-list(data.frame(a=1:2,b=2:3), >> data.frame(a=3:4,b=5:6),data.frame(a=7:8,b=9:10)) >> (mylist) >> >> # I want to grab only one specific column from each list element >> neededcolumns<-c(1,2,0) # number of the column I need from each element of >> the list >> >> # Below, I am doing it using a loop: >> newlist<-NULL >> for(i in 1:length(mylist) ) { >> newlist[[i]]<-mylist[[i]] [neededcolumns[i]] >> } >> newlist<-do.call(cbind,newlist) >> (newlist) >> >> I was wondering if there is any way to avoid the loop above and make it >> faster. >> In reality, I have a much longer list, each of my data frames is much >> larger and I have to do it MANY-MANY times. >> Thanks a lot! >> >> Dimitri Liakhovitski >> gfk.com <http://marketfusionanalytics.com/> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> >> > > > -- > Dimitri Liakhovitski > gfk.com <http://marketfusionanalytics.com/> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org 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. David Winsemius Alameda, CA, USA ______________________________________________ R-help@r-project.org 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.