【问题标题】:Is it possible to add percentages to a contingency table是否可以将百分比添加到列联表
【发布时间】:2013-03-29 18:58:17
【问题描述】:

我对 R 中的 table() 函数有疑问。我想添加一个额外的列来显示 table() 计数的百分比。我有一个这样的数据框:

delta=data.frame(x1=c("x001","x001","x002","x002","x001","x001","x002"),x2=c(1,2,1,1,1,1,1))

当我为这个数据框计算 table() 时,我得到了这个:

table(delta$x1,delta$x2)

       1 2
  x001 3 1
  x002 3 0

可以在此表中添加百分比,或者 R 中有任何函数或包可以计算如下:

       1 2  Number Percentage
  x001 3 1    4     0.5714286
  x002 3 0    3     0.4285714

感谢您的帮助。

【问题讨论】:

    标签: r


    【解决方案1】:

    计算不是很棘手。
    可能让您感到困惑的是该表不会直接转换为 data.frame。至少不是你想要的那样。这是一个分解,一步一步。

    # this is the basic table, we want it as a data.frame
    delCounts <- table(delta)
    
    # you need to convert the table to a matrix, before converting to a data.frame
    results <- data.frame(matrix(delCounts, nrow=nrow(delCounts)))
    
    # you may want to preserve the names.  Have a look: 
    dimnames(delCounts)  # first are the column names, then row names
    
    colnames(results) <- dimnames(delCounts)[[1]]
    rownames(results) <- dimnames(delCounts)[[2]]
    
    # Now sum up and take percentages
    # we can use vectorized arithmetic operations for the percentage
    results$Number <- rowSums(results)
    results$Percentage <- results$Number / sum(results$Number)
    
    # we might want to round instead
    results$Percentage <- round(results$Number / sum(results$Number)*100, 2)
    
    results
    #   x001 x002 Number Percentage
    # 1    3    1      4      57.14
    # 2    3    0      3      42.86
    

    【讨论】:

      【解决方案2】:

      这是使用sum()rowSums() 的快速解决方案:

      > tbl <- table(delta)
      > (tbl <- cbind(tbl, rowSums(tbl), rowSums(tbl) / sum(tbl)))
      
           1 2        
      x001 3 1 4 0.571
      x002 3 0 3 0.429
      

      你可以用colnames()添加列名;例如:

      > colnames(tbl) <- c("1", "2", "N", "Pct")
      > tbl
           1 2 N   Pct
      x001 3 1 4 0.571
      x002 3 0 3 0.429
      

      【讨论】:

      • 还可以添加一些带有cbind(tbl, Sums=rowSums(tbl), Pct=的列名...
      【解决方案3】:

      您可以使用prop.tableaddmargins

      tbl <- table(delta$x1,delta$x2)
      
      prop.table(tbl)
      
      # 1         2
      # x001 0.4285714 0.1428571 
      # x002 0.4285714 0.0000000
      
      addmargins(tbl)
      
      # 1 2 Sum
      # x001 3 1   4
      # x002 3 0   3
      # Sum  6 1   7
      

      编辑

      当然你可以做类似的事情

      rowSums(prop.table(tbl)) 
           x001      x002 
      0.5714286 0.4285714 
      

      但我的回答是说R中有一些内置函数可以完成table函数。

      【讨论】:

      • 我看不到 prop.table 如何自行解决 OP 的问题。你仍然需要总结结果,不是吗?
      • @RicardoSaporta 你可以添加rowSums..问题是关于R中任何计算东西的函数like这个......我希望我的答案在编辑后很清楚。
      • 我跟着你,你可以总结prop.table。我只是认为prop.tableaddmargins 不能很好地回答这个问题。即,你不能这样做cbind(addmargins(tbl), rowSums(prop.table(tbl)))
      • @RicardoSaporta no 试试这个,例如cbind(addmargins(tbl,2),rowSums(prop.table(tbl)))。但这并不重要,正如我之前所说,我的回答主要是向 OP 了解此类功能是否存在。 (感谢严谨)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多