[R] How to get a nested " list of lists of data.frames" into a data.frame or nested dataframe for easy viewing

Beckstrand, Janis, NCOD Janis.Beckstrand at va.gov
Thu Nov 21 22:34:20 CET 2013


I have data in a list of the form: list(list(dataframe())). It have
attached some random data with this structure called "l.Rdata".  The
structures in l.Rdata are as follows: a list of 4 groups: a list of 12
statistics: (and for each statistic, a dataframe contains (1row,3cols)
of values named "estimates", "lowerCL", "upperCL"). See the sample data
set attached.

I am using the following syntax to create this type of file:

require(epiR)
l<-lapply(seq_len(ncol(data[,])),function(i) {output<-epi.tests(g2[[i]],
conf.level = 0.95, verbose = TRUE)
    return(output)}) #The package "epi.tests" produces 12 statistics and
is in the epiR{}  program.

I need to get the information in my lists, like l.Rdata, into get a
data.frame or other format (using cat or xtable or something?) that is
easily readable and that  I see on the screen and save and print out,
and  that looks like
                                      group1
group2                         group3                        group4
Statistics	Est (lower,upper)        Est (lower,upper)      Est
(lower,upper)     Est (lower,upper)
aprev                     .32  (.29,.34)                        .31
(.27, .25)              etc	                               etc
tprev
etc
.
Statistic 12         .40 (.38, .42)
etc                          etc

I have looked at the various examples of how to get a list format to a
dataframe at
http://stackoverflow.com/questions/4512465/what-is-the-most-efficient-wa
y-to-cast-a-list-as-a-data-frame?rq=1 and at
http://stackoverflow.com/questions/4227223/r-list-to-data-frame

And (I think) I have attempted to use them all!, trying also to augment
each of them to convert the list of lists in the structure for output
that I need. 
After several days of trying, the structures I get out are still not
conducive to getting the data into a data.frame format where I  have
12 observations(statistics) of 12 (4 x 3) numbers that I can label or
nest into 4 groups to view on a screen or print out.  

I also have a second problem which I think it related.  As shown above,
I want to collapse the columns named "lower" and "upper" in l.Rdata into
to single "Confidence Interval (CI)" column,  within each of the groups
and add the formatting (  ,   ) to the column.  But my syntax for this
doesn't work either. 
Even when I pull out a list for a single group...(like group1) and
redefine it as.data.frame (using as.matrix, rbind, unlist, etc), I am
not getting the syntax "paste(group1[,2},group1[,3],collapse=", ") " to
collapse the rows in columns 2 and 3, even though I think this syntax is
exactly like syntax that I have used successfully before. (I copied the
syntax out of a program that has run fine before). I have looked for and
tried other options such as concatenate, sprint, etc. I think this
second problem is due to the fact that I still have a "list" structure
hidden in my attempts at transforming the structures in l.Rdata.

Any help is appreciated.  Thanks Jan

Jan Beckstrand 


More information about the R-help mailing list