[R] Extracting multiple elements from a list

Julian Taylor julian.taylor at adelaide.edu.au
Fri Jan 16 06:20:45 CET 2004



"Waichler, Scott R" wrote:
> 
> Brian described well the operation I would like to do.
> I'm not familiar with do.call() but I'll work on that.
> Yes, ideally I would like to access values throughout a list object
> with fully implict indexing, such as the invalid "alist[[1:2]]$vec[c(2, 4)]".
> Notice I was hoping to subset anywhere in the data structure.
> Since I can't do this subsetting with indexing directly, I was looking for
> handy (and hopefully fast) functions that could be defined
> generically and then called with arguments.  The use of sapply()
> and lapply() with function(i) seem promising, but do not quite
> cover the functionality I was looking for.
> 

The functions sapply() and lapply() have more generality that has been
overlooked in this thread.

This will answer your first question.

> unlist(lapply(alist, function(x, ind = c(2,4)) x$vec[ind]))
[1] 2 4 6 8
    
hth,
Julian

-- 
---
Julian Taylor			phone: +61 8 8303 6751
ARC Research Associate            fax: +61 8 8303 6760
BiometricsSA,                  mobile: +61 4 1638 8180  
University of Adelaide/SARDI    email: julian.taylor at adelaide.edu.au
Private Mail Bag 1                www:
http://www.BiometricsSA.adelaide.edu.au
Glen Osmond SA 5064

"There is no spoon."   -- Orphan boy  
---




More information about the R-help mailing list