[R] Selecting/Accessing the last vector in a list of a list of data.frames

Bryan Hanson hanson at depauw.edu
Wed Aug 12 01:59:24 CEST 2009


Thanks Henrique.  I would have not thought of the syntax you suggest, though
it embodies the sort of multilevel (not quite recursive) application of
lapply I was thinking of.  However, it returns ³test² with V2 missing,
everything else intact.  Strange; I can't really state in words what I think
it should do, much less what it does do!

I think an easier approach for me will be to re-write the function that
generates "test" so it is simpler to extract what I need.  I will think on
it.

Thanks, Bryan

> temp <- lapply(test, lapply, '[', 'V1')
> str(temp)
List of 2
 $ G:List of 4
  ..$ cls:'data.frame':    101 obs. of  1 variable:
  .. ..$ V1: num [1:101] -0.0019 -0.0019 -0.00189 -0.00188 -0.00186 ...
  ..$ rob:'data.frame':    101 obs. of  1 variable:
  .. ..$ V1: num [1:101] -0.00142 -0.00141 -0.0014 -0.00139 -0.00137 ...
  ..$ c  : num NA
  ..$ r  : num NA
 $ T:List of 4
  ..$ cls:'data.frame':    101 obs. of  1 variable:
  .. ..$ V1: num [1:101] -0.00222 -0.00222 -0.00221 -0.00219 -0.00216 ...
  ..$ rob:'data.frame':    101 obs. of  1 variable:
  .. ..$ V1: num [1:101] -0.000981 -0.000979 -0.000972 -0.000961 -0.000946
..
  ..$ c  : num NA
  ..$ r  : num NA


On 8/11/09 7:28 PM, "Henrique Dallazuanna" <wwwhsd at gmail.com> wrote:

> If I understand correctly your question, you can try something about like
> this:
> 
> # Access all elements named 'V1' in your list
> lapply(test, lapply, '[', 'V1')
> 
> 
> On Tue, Aug 11, 2009 at 3:49 PM, Bryan Hanson <hanson at depauw.edu> wrote:
>> Hello Again R Folks:
>> 
>> I¹m trying to clean up some code.  Suppose I have an object like this:
>> 
>>> str(test)
>> List of 2
>>  $ G:List of 2
>>   ..$ cls:'data.frame':    101 obs. of  2 variables:
>>   .. ..$ V1: num [1:101] -0.0019 -0.0019 -0.00189 -0.00188 -0.00186 ...
>>   .. ..$ V2: num [1:101] 0.000206 0.000247 0.000288 0.000329 0.000371 ...
>>   ..$ rob:'data.frame':    101 obs. of  2 variables:
>>   .. ..$ V1: num [1:101] -0.00142 -0.00141 -0.0014 -0.00139 -0.00137 ...
>>   .. ..$ V2: num [1:101] 0.000424 0.000456 0.000487 0.000517 0.000546 ...
>>  $ T:List of 2
>>   ..$ cls:'data.frame':    101 obs. of  2 variables:
>>   .. ..$ V1: num [1:101] -0.00222 -0.00222 -0.00221 -0.00219 -0.00216 ...
>>   .. ..$ V2: num [1:101] -0.00077 -0.000742 -0.000712 -0.000681 -0.000648
>> ..
>>   ..$ rob:'data.frame':    101 obs. of  2 variables:
>>   .. ..$ V1: num [1:101] -0.000981 -0.000979 -0.000972 -0.000961 -0.000946
>> ..
>>   .. ..$ V2: num [1:101] -0.000332 -0.000303 -0.000274 -0.000245 -0.000216
>> ..
>> 
>> I need to perform some operations on each value of V1 in turn, then each
>> value of V2 in turn (so for instance I want test$G$cls$V1).  The structure
>> of this object is nearly constant except the first elements of the list (G,
>> T in the example) may vary in number and name, so I need something that
>> accommodates this.
>> 
>> I can do this with loops, but it seems like a job for lapply or rapply, but
>> these don't quite work.  I've played with quite a few variations, searched
>> the help archives and found a number of useful ideas, but not quite what I
>> need.  The only thing that nearly works is do.call(cbind, object) enough
>> times to bring V1 and V2 "to the surface" but then I've lost my carefully
>> constructed naming.
>> 
>> Any suggestions appreciated.  It seems like there might be a simple
>> approach, but I may be too tired right now to see it!
>> 
>> Thanks, Bryan
>> *************
>> Bryan Hanson
>> Professor of Chemistry & Biochemistry
>> DePauw University, Greencastle IN USA
>> 
>> ______________________________________________
>> R-help at 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.
> 
> 




More information about the R-help mailing list