Bryan Hanson
2009-Aug-11 18:49 UTC
[R] Selecting/Accessing the last vector in a list of a list of data.frames
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
Henrique Dallazuanna
2009-Aug-11 23:28 UTC
[R] Selecting/Accessing the last vector in a list of a list of data.frames
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@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]