问题
In the table below the column named "Percent" shows the total column percent. How do I get it to show the column percent of each level of "am" within each level of "vs"?
This is what I've got:

This is what I'm looking for:

Knitr chunk below:
<<echo=FALSE,results='asis'>>=
#
# library(tables)
# library(Hmisc)
# library(Formula)
## This gives me column percentages for the total table.
latex( tabular( Factor(vs)*Factor(am) ~ gear*Percent("col"), data=mtcars ) )
## I am trying to get column percentages for each level of "vs"
@
回答1:
I think you would need to change your formula to do this. Like this for example:
tabular(Factor(vs) ~ gear*Percent("row")*Factor(am), data = mtcars)
# gear
# Percent
# am
#vs 0 1
#0 66.67 33.33
#1 50.00 50.00
回答2:
You can use the Equal()
pseudofunction for the denom option to make levels of factor vs the denominator.
tabular( Factor(vs)*Factor(am) ~ gear*Percent(denom = Equal(vs)), data=mtcars)
gear
vs am Percent
0 0 66.67
1 33.33
1 0 50.00
1 50.00
来源:https://stackoverflow.com/questions/19319825/in-the-tables-package-how-to-get-column-percentages-of-a-subset-of-a-variable