【问题标题】:In the "Tables"-package: How to get column percentages of a subset of a variable?在“表格”包中:如何获取变量子集的列百分比?
【发布时间】:2020-09-07 22:37:07
【问题描述】:

在下表中,名为“百分比”的列显示了总列百分比。如何让它在每个级别的“vs”中显示每个级别的“am”的列百分比?

这就是我所拥有的:

这就是我要找的:

下面的编织块:

<<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"

@

【问题讨论】:

  • 顺便说一句,你是怎么想出 46.15 的?
  • 在 excel 中从 Counts/total 快速计算,可能是错误的????

标签: r knitr


【解决方案1】:

我认为您需要更改公式才能做到这一点。比如这样:

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】:

    您可以对 denom 选项使用 Equal() 伪函数来确定因子与分母的水平。

    library(tables) 
    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
    

    reprex package (v0.3.0) 于 2020 年 9 月 7 日创建

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2019-05-18
      • 2020-10-28
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      相关资源
      最近更新 更多