Skip to content
Prev 317431 / 398513 Next

grabbing from elements of a list without a loop

One could try using unlist(mylist, recursive=FALSE) to make a list of all the columns
of the data.frames in mylist and subscripting from that.  E.g., here is some barely tested
code:
  > nCols <- vapply(mylist, ncol, 0L)
  > neededcolumns<-c(1,2,0) # I assume 0 means no column wanted from 3rd df in list
  > i <- neededcolumns + c(0, cumsum(nCols[-length(nCols)]))
  > i <- i[neededcolumns>0]
  > data.frame(unlist(mylist, recursive=FALSE)[i])
    a b
  1 1 5
  2 2 6


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com