s3 有没有办法将 prop.table 组合为字符变量?

s3 is there a way to combine prop.table for character variables?

菜鸟,我一直在尝试使用 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 

也许这就是您想要的。每个类别中的值总和为 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