如何获取调查包中频率table的百分比
How to get the percentage of the frequency table in survey package
我正在进行一项复杂的调查。我正在尝试使用 svytable
来做应急 table。我使用了以下代码:
(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
我想要 table 中每个单元格的百分比。类似下面的内容,
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%)
感谢任何帮助
一种简单的方法是将 table 对象的值替换为原始值和使用 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%)
我正在进行一项复杂的调查。我正在尝试使用 svytable
来做应急 table。我使用了以下代码:
(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
我想要 table 中每个单元格的百分比。类似下面的内容,
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%)
感谢任何帮助
一种简单的方法是将 table 对象的值替换为原始值和使用 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%)