Using R, I want to create a table with nested columns (and possibly nested rows). The cell values should include frequencies and within-sub-group totals and percentages (i.e., cell/[sub-group row total]*100).
I would prefer that the solution uses the tables package. I will be outputting to LaTeX.
Three questions:
1) Please explain why I'm getting the following error: Error in Percent("row") : Summary fn not allowed with Percent
library(tables)
set.seed(123)
df <- data.frame(exposure = sample(LETTERS[1:5], 100, TRUE),
Group = sample(c("GroupX","GroupY"), 100, TRUE),
disease = as.integer(sample(c(0,1), 100, TRUE)))
num <- function(x) base::sum(x, na.rm=TRUE)
tabular(Factor(exposure)+1~
Factor(Group)*
(Heading()*num*Heading(One)*disease*
((Total=1)+Percent("row"))),
data=df)
2) How can I create the following ideal table WITH additional columns for within-group percentages after each group*disease frequency. Note that persons without disease are not included in the table.
Group
GroupX GroupY
num num
exposure Total disease Total disease
A 9 4 13 6
B 12 4 9 5
C 9 8 9 6
D 7 1 8 3
E 9 4 15 12
All 46 21 54 32
Here is a start:
tabular(Factor(exposure) + 1 ~
Factor(Group) *
((Total = 1) + num * disease), data = df)
3) The package uses Percent()
. Why would one use a logical vector with Percent()
. Can you give an example? Would using a logical vector help me with this problem?
This is similar to this question; however, the offered answer calculates incorrect percentages as evidenced by an example with more than 2 columns.