【问题标题】:s3 is there a way to combine prop.table for character variables?s3 有没有办法为字符变量组合prop.table?
【发布时间】: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


【解决方案1】:

也许这就是你想要的。每个类别的值总和为 100%。

lis <- sapply( Student1995, function(x) t( sapply( x, table ) ) )

sapply( lis, function(x) colSums(prop.table(x)) )
$alcohol
                  Not  Once.or.Twice.a.week          Once.a.month
                  0.0                   0.6                   0.4
          Once.a.week More.than.once.a.week
                  0.0                   0.0

$drugs
       Not Tried.once Occasional    Regular
       0.8        0.2        0.0        0.0

$smoke
       Not Occasional    Regular
       0.6        0.2        0.2

$sport
Not.regular     Regular
        0.4         0.6

以及整个摘要......

prop.table( table(as.vector( sapply( Student1995, unlist ))) )

                 Not          Not regular           Occasional
                0.35                 0.10                 0.05
        Once a month Once or Twice a week              Regular
                0.10                 0.15                 0.20
          Tried once
                0.05

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多