[R] How to convert a 3 dimensional List to make a table with tables tabular()?
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Nov 19 12:27:46 CET 2013
On 13-11-18 3:46 PM, Beckstrand, Janis, NCOD wrote:
> 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 lower upper
>
> 1 0.2263884 0.2244714 0.228314
>
>
>
> $tprev
>
> est lower upper
>
> 1 0.204388 0.2025413 0.2062442
>
>
>
> Statslow$i10
>
> $aprev
>
> est lower upper
>
> 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
>
> tprev 0.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 L2 L1
>
> 1 est 0.2263884 aprev i9
>
> 2 lower 0.2244714 aprev i9
>
> 3 upper 0.2283140 aprev i9
>
> 4 est 0.2043880 tprev i9
>
> 5 lower 0.2025413 tprev i9
>
> 6 upper 0.2062442 tprev 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 value L2 L1
>
> 1 est 0.2263884 aprev i9
>
> 2 lower 0.2244714 aprev i9
>
> 3 upper 0.2283140 aprev i9
>
> 4 est 0.2043880 tprev i9
>
> 5 lower 0.2025413 tprev i9
>
> 6 upper 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.
>
The arguments to tabular are in the wrong order. You need to give the
formula first or name it as "table". So
tabular(L2 ~ L1*variable, data=statslow.d)
would not give an error, but it probably doesn't do what you want,
because you don't specify a statistic, so it will default to a count.
You could use the "mean" function, e.g.
tabular(L2 ~ L1*variable*Heading()*mean, data=statslow.d)
(The Heading() will suppress the column label "mean".) There are
examples in the vignette that do fancy formatting of multiple columns,
so it would be possible to write the interval as [.224, .228] for instance.
Duncan Murdoch
More information about the R-help
mailing list