【问题标题】:How to get the percentage of the frequency table in survey package如何获取调查包中频率表的百分比
【发布时间】:2019-10-11 03:57:07
【问题描述】:

我正在进行一项复杂的调查。我正在尝试使用svytable 来做列联表。我使用了以下代码:

(tbl <- svytable(~sch.wide+stype, dclus1))

result
        stype
sch.wide         E         H         M
     No   406.1640  101.5410  270.7760
     Yes 4467.8035  372.3170  575.3989

我想知道表格中每个单元格的百分比。类似于以下内容,

      stype
sch.wide         E         H         M
     No   406.1640(xx%)  101.5410(xx%)  270.7760(xx%)
     Yes 4467.8035(xx%)  372.3170(xx%)  575.3989(xx%)

感谢任何帮助

【问题讨论】:

    标签: r survey


    【解决方案1】:

    一种简单的方法是使用paste0() 将表格对象的值替换为原始值和比例:

    library(survey)
    data(api)
    xtabs(~sch.wide+stype, data=apipop)
    dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
    tbl <- svytable(~sch.wide+stype, dclus1)
    
    tbl[] <- paste0(round(tbl, 2), " (", round(prop.table(tbl)*100, 2), "%)")
    tbl
    
            stype
    sch.wide E               H              M             
         No  406.16 (6.56%)  101.54 (1.64%) 270.78 (4.37%)
         Yes 4467.8 (72.13%) 372.32 (6.01%) 575.4 (9.29%) 
    

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 2017-02-15
      • 2021-11-18
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多