In the “Tables”-package: How to get column percentages of a subset of a variable?

房东的猫 提交于 2019-12-24 00:36:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!