【问题标题】:Getting both column counts and proportions in the same table in R在 R 中的同一个表中获取列数和比例
【发布时间】:2012-02-24 21:18:23
【问题描述】:

如果有一个函数可以在同一张表中同时提供计数和列/总体百分比?我可以查看两个表和 reshape2,但看不到这样做的选项。我举个小例子:

数据设置

n <- 100
x <- sample(letters[1:3], n, T)
y <- sample(letters[1:3], n, T)
d <- data.frame(x=x, y=y)

有桌子

这非常笨拙,因为它需要我取消列出并重新组合。

> library(tables)
> (t1 <- tabular(x~y*(n=length), d))

   a  b  c 
 x n  n  n 
 a 13 14 11
 b  8 11 13
 c 10 12  8
> prop.table(matrix(unlist(t1),3,3), 1)
          [,1]      [,2]      [,3]
[1,] 0.3421053 0.3684211 0.2894737
[2,] 0.2500000 0.3437500 0.4062500
[3,] 0.3333333 0.4000000 0.2666667

使用 Reshape2

这有点容易,但仍然不是一个。

> library(reshape2)
> (t2 <- acast(d, x~y, length))
Using y as value column: use value_var to override.
   a  b  c
a 13 14 11
b  8 11 13
c 10 12  8
> (t3 <- prop.table(t2,1))
          a         b         c
a 0.3421053 0.3684211 0.2894737
b 0.2500000 0.3437500 0.4062500
c 0.3333333 0.4000000 0.2666667

期望的输出

我真正想要的是看起来像这样的输出:

> structure(list(
+     a = data.frame(n=t2[,1], pct=t3[,1]),
+     b = data.frame(n=t2[,2], pct=t3[,2]),
+     c = data.frame(n=t2[,3], pct=t3[,3])), 
+   class = 'data.frame',
+   row.names = letters[1:3])
  a.n     a.pct b.n     b.pct c.n     c.pct
a  13 0.3421053  14 0.3684211  11 0.2894737
b   8 0.2500000  11 0.3437500  13 0.4062500
c  10 0.3333333  12 0.4000000   8 0.2666667

有没有办法用 R 轻松做到这一点?

【问题讨论】:

  • 你得到的是 row-pcts 而不是 prop.table 索引为 1 的 col-pcts

标签: r


【解决方案1】:

这是一种方法,您仍然需要第二步,但它位于 tabular 命令之前,因此结果仍然是 tabular 对象。

n <- 100 
x <- sample(letters[1:3], n, T) 
y <- sample(letters[1:3], n, T) 
d <- data.frame(x=x, y=y) 
d$z <- 1/ave( rep(1,n), d$x, FUN=sum )

(t1 <- tabular(x~y*Heading()*z*((n=length) + (p=sum)), d))

【讨论】:

  • 我认为这是迄今为止最干净的解决方案。我个人不太喜欢tabular 输出,但它确实做得很好。
【解决方案2】:

使用 gmodles 包中的 CrossTable 函数。

library(gmodels)

检查CrossTable的参数

args(CrossTable)
function (x, y, digits = 3, max.width = 5, expected = FALSE, 
    prop.r = TRUE, prop.c = TRUE, prop.t = TRUE, prop.chisq = TRUE, 
    chisq = FALSE, fisher = FALSE, mcnemar = FALSE, resid = FALSE, 
    sresid = FALSE, asresid = FALSE, missing.include = FALSE, 
    format = c("SAS", "SPSS"), dnn = NULL, ...) 
NULL

申请CrossTable

CrossTable(x=d$x, y=d$y)



   Cell Contents
|-------------------------|
|                       N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|


Total Observations in Table:  100 


             | d$y 
         d$x |         a |         b |         c | Row Total | 
-------------|-----------|-----------|-----------|-----------|
           a |        13 |        12 |         8 |        33 | 
             |     0.182 |     0.306 |     0.924 |           | 
             |     0.394 |     0.364 |     0.242 |     0.330 | 
             |     0.371 |     0.387 |     0.235 |           | 
             |     0.130 |     0.120 |     0.080 |           | 
-------------|-----------|-----------|-----------|-----------|
           b |        13 |        11 |        18 |        42 | 
             |     0.197 |     0.313 |     0.969 |           | 
             |     0.310 |     0.262 |     0.429 |     0.420 | 
             |     0.371 |     0.355 |     0.529 |           | 
             |     0.130 |     0.110 |     0.180 |           | 
-------------|-----------|-----------|-----------|-----------|
           c |         9 |         8 |         8 |        25 | 
             |     0.007 |     0.008 |     0.029 |           | 
             |     0.360 |     0.320 |     0.320 |     0.250 | 
             |     0.257 |     0.258 |     0.235 |           | 
             |     0.090 |     0.080 |     0.080 |           | 
-------------|-----------|-----------|-----------|-----------|
Column Total |        35 |        31 |        34 |       100 | 
             |     0.350 |     0.310 |     0.340 |           | 
-------------|-----------|-----------|-----------|-----------|

【讨论】:

    【解决方案3】:
    tbl <- with(d, table(x,y)  )
     pct.tbl <- prop.table(tbl)
     colnames(pct.tbl) <- paste("pct",colnames(pct.tbl), sep=".") 
    # The next line constructs an interleaving index to rearrange the columns
     cbind(tbl, pct.tbl)[, c( matrix(1:(2*ncol(tbl)), nrow=2, byrow=TRUE) )]
    #------
       a pct.a  b pct.b  c pct.c
    a 11  0.11 10  0.10 12  0.12
    b  6  0.06 11  0.11 11  0.11
    c 12  0.12 11  0.11 16  0.16
    

    另一种进行交织的方法是使用c 拉出转置矩阵序列

    c( t( matrix(1:(2*ncol(tbl)), ncol=2) ) )
    #[1] 1 4 2 5 3 6
    

    如果您希望这些比例是列百分比,那么只需在 prop.table 调用中的 'tbl' 参数之后添加一个 2

     prop.table(tbl,2)
     #----------
       y
    x           a         b         c
      a 0.3793103 0.3125000 0.3076923
      b 0.2068966 0.3437500 0.2820513
      c 0.4137931 0.3437500 0.4102564
    

    【讨论】:

      猜你喜欢
      • 2019-09-04
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      相关资源
      最近更新 更多