【发布时间】:2022-01-12 02:38:00
【问题描述】:
这里是菜鸟,我一直在尝试使用 S3 来汇总一个 data.frame 的比例数据,其中有四列字符数据。我的目标是构建一种汇总方法,以同时显示每个变量的每个级别的比例。
我可以看到如何获得每列的比例
a50survey1 <- table(Student1995$alcohol)
a50survey2 <- table(Student1995$drugs)
a50survey3 <- table(Student1995$smoke)
a50survey4 <- table(Student1995$sport)
prop.table(a50survey1)
prop.table(a50survey1)
Not Once or Twice a week Once a month Once a week More than once a week
0.10 0.32 0.24 0.28 0.06
但我找不到将所有prop.table 输出组合成一个摘要输出的方法。
除非我真的错了。我找不到像summary.prop.table 这样适合我的 S3 方法。目标是为当前数据框设置,然后在未来放入新的相同大小和观察数据框。
我真的是一个循序渐进的人,如果你能帮助我,那就太好了 - 谢谢
数据框信息在这里。共有四列,每列都有不同数量的观测分类选项。
> dput(head(Student1995,5))
structure(list(alcohol = structure(c(3L, 2L, 2L, 2L, 3L), .Label = c("Not",
"Once or Twice a week", "Once a month", "Once a week", "More than once a week"
), class = "factor"), drugs = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("Not",
"Tried once", "Occasional", "Regular"), class = "factor"), smoke = structure(c(2L,
3L, 1L, 1L, 1L), .Label = c("Not", "Occasional", "Regular"), class = "factor"),
sport = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Not regular",
"Regular"), class = "factor")), row.names = c(NA, 5L), class = "data.frame")
汇总数据(如果有帮助) - 编辑
> summary(Student1995)
alcohol drugs smoke sport
Not : 5 Not :36 Not :38 Not regular:13
Once or Twice a week :16 Tried once: 6 Occasional: 5 Regular :37
Once a month :12 Occasional: 7 Regular : 7
Once a week :14 Regular : 1
More than once a week: 3
【问题讨论】:
-
@Andre Wildberg 你不知道我在这上面花了多少小时!!我试图结合打印(Prop.table)和许多其他废话!非常感谢!!
标签: r summary r-s3 proportions