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

2013-11-21 Thread Beckstrand, Janis, NCOD
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 group3group4
Statistics  Est (lower,upper)Est (lower,upper)  Est
(lower,upper) Est (lower,upper)
aprev .32  (.29,.34).31
(.27, .25)  etcetc
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 
__
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.


[R] How to convert a 3 dimensional List to make a table with tables tabular()?

2013-11-18 Thread Beckstrand, Janis, NCOD
I have information in a 3 dimensional list that I want to use to create
a table for a written report. 

 

The 3d list was created by applying the epiR function  epitest  to a
list of 8 2x2 tables using  lapply.  

The resulting list gives the 12 statistics with CIs,  that  generated by
epitest, for each of the 8 matrices (i9, i10, etc). 

 

The list has the following form:

 

Statslow$i9

$aprev

est lowerupper

1 0.2263884 0.2244714 0.228314

 

$tprev

   est lower upper

1 0.204388 0.2025413 0.2062442

 

Statslow$i10

$aprev

est lowerupper

1 0.227004 0.22545  0.228314

 

$tprev

   est lower upper

1 0.204388 0.2025413 0.2062442

 

 

I want to create a report that has a table in the following format:

   I9
i10  

 Est   lower upper   Est  lower
upper

 

aprev   0.23 0.22 0.23   etc

tprev0.2   0.20 0.21

etc

 

I have read and tried the tables package pdf and also the information at

http://www.r-statistics.com/tag/data-frame/ about cast() and
tabular.cast_df.r(), 

and also all the information I can find on nested tables, but have not
be successful.  

I have tried to use melt() on the 3D list (to create a dataframe
statslow.d)  but get what seems more like S3 output :

 

  variable value   L2L1

1  est   0.2263884aprev   i9

2lower0.2244714aprev  i9

3upper   0.2283140aprev   i9

4  est   0.2043880 tprev  i9

5lower0.2025413tprev   i9

6upper   0.2062442tprev   i9

 

When I try to use it with tables() I get errors

and I can't see how to use the tables function on it to get what I want.

Here is the tables() code that I have tried:

 

class(statslow.d)

[1] data.frame

 head(statslow.d)

  variable valueL2  L1

1  est 0.2263884 aprev  i9

2lower 0.2244714 aprev i9

3upper 0.2283140 aprev i9

4  est 0.2043880 tprev i9

5lower 0.2025413 tprev i9

6upper 0.2062442 tprev i9

 tabular(statslow.d,L2~L1*variable)

Error in tabular.formula(as.formula(table, env = parent.frame()), ...) :


  data must be a dataframe, list or environment

 statslow.d-as.data.frame(statslow.d)

 tabular(statslow.d,L2~L1*variable)

Error in tabular.formula(as.formula(table, env = parent.frame()), ...) :


  data must be a dataframe, list or environment

 

I would appreciate any help or hints anyone can give.

 

Jan Beckstrand

 


[[alternative HTML version deleted]]

__
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.