In the “Tables”-package: How to get column percent

2019-07-19 16:49发布

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:

hjyygh

This is what I'm looking for:

enter image description here

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"

@

标签: r knitr
2条回答
我命由我不由天
2楼-- · 2019-07-19 17:33

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  
查看更多
Evening l夕情丶
3楼-- · 2019-07-19 17:45

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
查看更多
登录 后发表回答