一个表中两个数据集的分类汇总统计(比较)

Mat*_*ttV 2 r

我在制作一个表格时遇到了问题,该表格总结了我的两个数据集及其分类变量,这是一个我在各种论文中经常看到的格式的表格.

问题如下,我有两个数据集(一个比另一个过滤得多一点),我想并排显示他们的分类汇总统计数据.使用两个数据集:

A <- head(mtcars[, c(2, 8:11)])

                  cyl vs am gear carb
Mazda RX4           6  0  1    4    4
Mazda RX4 Wag       6  0  1    4    4
Datsun 710          4  1  1    4    1
Hornet 4 Drive      6  1  0    3    1
Hornet Sportabout   8  0  0    3    2
Valiant             6  1  0    3    1

B <- head(mtcars[3:6, c(2, 8:11)])
                  cyl vs am gear carb
Datsun 710          4  1  1    4    1
Hornet 4 Drive      6  1  0    3    1
Hornet Sportabout   8  0  0    3    2
Valiant             6  1  0    3    1
Run Code Online (Sandbox Code Playgroud)

我想像这样提供摘要统计:

                           Table A                  Table B
  Variable Levels Count Column N %  Levels Count Column N %
1       cyl      4     1      16.67      4     1         25
2                6     4      66.67      6     2         50
3                8     1      16.67      8     1         25
4        vs      0     3         50      0     1         25
5                1     3         50      1     3         75
6        am      0     3         50      0     3         75
7                1     3         50      1     1         25
8      gear      3     3         50      3     3         75
9                4     3         50      4     1         25
10     carb      1     3         50      1     3         75
11               2     1      16.67      2     1         25
12               4     2      33.33      4     0          0
Run Code Online (Sandbox Code Playgroud)

我已经能够使用本帖中描述关于汽车数据集的prettyTable.R 来接近我想要的东西,但是我很难根据我的需要调整这个片段:

Roman Luštrik:
Here's my solution. It ain't pretty, which is why I put a bag over its head (wrap it in     a function). I also add another variable to demonstrate that it's general (I hope).

prettyTable <- function(x) {

  tbl <- apply(x, 2, function(m) {
    marc <- sort(unique(m))
    cnt <- matrix(table(m), ncol = 1)
    out <- cbind(marc, cnt)
    out <- out[order(marc), ] # do sorting
    out <- cbind(out, round(prop.table(out, 2)[, 2] * 100, 2))
  })

  x2 <- do.call("rbind", tbl)

  spaces <- unlist(lapply(apply(x, 2, unique), length))
  space.names <- names(spaces)
  spc <- rep("", sum(spaces))
  ind <- cumsum(spaces)
  ind <- abs(spaces - ind)+1
  spc[ind] <- space.names

  out <- cbind(spc, x2)
  out <- as.data.frame(out)

  names(out) <- c("Variable", "Levels", "Count", "Column N %")
  out
}
Run Code Online (Sandbox Code Playgroud)

我已经能够(部分地)通过cbinding这个prettyTable的输出来做到这一点:

cbind(prettyTable(A)[1:11,],prettyTable(B))
Run Code Online (Sandbox Code Playgroud)

这种方法中的一些问题:注意第一个prettyTable中的1:11部分:此代码无法识别两个数据集中出现的不同数量的级别.遗憾的是,我没有足够的技能来识别需要添加/调整的代码,以便在不进行手动编辑的情况下获得所需的结果.

此外,prettyTable.R片段不接受我的分类变量,如果它们是因素,给我一个错误(我认为)引用代码中的prop.table函数.要复制该情境,请在制作prettyTable之前添加以下代码.

A$cyl <- as.factor(A$cyl)
B$cyl <- as.factor(B$cyl)
prettyTable(A)
Error in FUN(newX[, i], ...) : invalid 'type' (character) of argument
Run Code Online (Sandbox Code Playgroud)

最后,该功能在汇总时不接受一列数据.这不一定适用于我的情况,但我猜这样的片段对其他人也有用,如果它也有这个功能.

prettyTable(A$cyl)
Error in apply(x, 2, function(m) { : dim(X) must have a positive length
Run Code Online (Sandbox Code Playgroud)

非常感谢你的任何帮助,我一直在试图弄清楚这一点,但我一直无法自己这样做.

G. *_*eck 5

以下是一些方法:

Hmisc 查看?summary.formula更多信息.

library(Hmisc)
AB <- rbind(cbind(A, Table = "A"), cbind(B, Table = "B"))
s <- summary(Table ~., data = AB, method = "reverse")
print(s, exclude1 = FALSE)
Run Code Online (Sandbox Code Playgroud)

赠送:

Descriptive Statistics by Table

+--------+-------+-------+
|        |A      |B      |
|        |(N=6)  |(N=4)  |
+--------+-------+-------+
|cyl : 4 |17% (1)|25% (1)|
+--------+-------+-------+
|    6   |67% (4)|50% (2)|
+--------+-------+-------+
|    8   |17% (1)|25% (1)|
+--------+-------+-------+
|vs : 0  |50% (3)|25% (1)|
+--------+-------+-------+
|    1   |50% (3)|75% (3)|
+--------+-------+-------+
|am : 0  |50% (3)|75% (3)|
+--------+-------+-------+
|    1   |50% (3)|25% (1)|
+--------+-------+-------+
|gear : 3|50% (3)|75% (3)|
+--------+-------+-------+
|    4   |50% (3)|25% (1)|
+--------+-------+-------+
|carb : 1|50% (3)|75% (3)|
+--------+-------+-------+
|    2   |17% (1)|25% (1)|
+--------+-------+-------+
|    4   |33% (2)| 0% (0)|
+--------+-------+-------+
Run Code Online (Sandbox Code Playgroud)

tableone

library(tableone)
AB.fac <- replace(AB, TRUE, lapply(AB, factor)) # AB is from above
tableOne <- CreateCatTable(vars = names(AB)[-6], strata = "Table", data = AB.fac)
print(tableOne, showAllLevels = TRUE, test = FALSE)
Run Code Online (Sandbox Code Playgroud)

赠送:

          Stratified by Table
           level A         B        
  n              6         4        
  cyl (%)  4     1 (16.7)  1 (25.0) 
           6     4 (66.7)  2 (50.0) 
           8     1 (16.7)  1 (25.0) 
  vs (%)   0     3 (50.0)  1 (25.0) 
           1     3 (50.0)  3 (75.0) 
  am (%)   0     3 (50.0)  3 (75.0) 
           1     3 (50.0)  1 (25.0) 
  gear (%) 3     3 (50.0)  3 (75.0) 
           4     3 (50.0)  1 (25.0) 
  carb (%) 1     3 (50.0)  3 (75.0) 
           2     1 (16.7)  1 (25.0) 
           4     2 (33.3)  0 ( 0.0) 
Run Code Online (Sandbox Code Playgroud)

修订添加了tableone解决方案.