Skip to content
Prev 317424 / 398513 Next

grabbing from elements of a list without a loop

The answer is essentially no, a loop is required (as best I can see),
although it can be in the form of an apply type call instead.  Also,
your code will fail with a 0 index. Something like this should work:

newlist <- lapply(1:3,function(i)if(!neededcolumns[i])NULL else
mylist[[c(i,neededcolumns[i])]])

## note the use of [[c(i,j)]]  form for selecting columns as an
element from a list of lists

## Note that your cbind call produces a matrix, not a list.

-- Bert


You might wish to check the parallel package, as this looks like the
sort of thing parallellization could be profitably used for; but I
have no experience to offer beyond that suggestion.

-- Bert

On Tue, Feb 12, 2013 at 1:33 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote: