计算多个变量的水平和return表格结果

Calculate the levels of mutiple variables and return tabular result

我想将 summary 命令的输出放入数据 table 中。例如,使用此数据框:

   Person     V1     V2     V3     V4
1       A medium medium medium   high
2       B medium medium    low    low
3       V   high   high medium medium
4       D medium medium    low   high
5       E   high   high medium    low
6       F medium medium    low    low
7       G   high   high    low   high
8       H medium    low medium    low
9       I medium medium    low medium
10      J medium    low medium    low

x.df<-structure(list(Person = structure(c(1L, 2L, 10L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L), .Label = c("A", "B", "D", "E", "F", "G", "H", 
"I", "J", "V"), class = "factor"), V1 = structure(c(2L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("high", "medium"), class = "factor"), 
V2 = structure(c(3L, 3L, 1L, 3L, 1L, 3L, 1L, 2L, 3L, 2L), .Label = c("high", 
"low", "medium"), class = "factor"), V3 = structure(c(2L, 
1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("low", "medium"
), class = "factor"), V4 = structure(c(1L, 2L, 3L, 1L, 2L, 
2L, 1L, 2L, 3L, 2L), .Label = c("high", "low", "medium"), class = "factor")), .Names = c("Person", 
"V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, 
-10L))

summary(x.df) 我得到每个因子水平的计数:

     Person       V1         V2         V3         V4   
 A      :1   high  :3   high  :3   low   :5   high  :3  
 B      :1   medium:7   low   :2   medium:5   low   :5  
 D      :1              medium:5              medium:2  
 E      :1                                              
 F      :1                                              
 G      :1                                              
 (Other):4                                              

理想情况下,我想要每个因子水平的计数数据框,即:

  Var low medium high
1  V1   0      7    3
2  V2   2      5    3
3  V3   5      5    0
4  V4   5      2    3

行总和等于 10。

这是一种将每个问题变量的计数放入矩阵的方法。

myMat <- sapply(x.df[-1],
                function(x) table(factor(x, levels=c("low", "medium", "high"))))

想法是通过这些变量中的每一个使用sapply到运行,将变量转换为具有所需水平的因子,然后在转换后的变量上调用table .

这个returns

myMat
       V1 V2 V3 V4
low     0  2  5  5
medium  7  5  5  2
high    3  3  0  3

如果你想把它转换成你想要的输出,只需使用t转置它:

t(myMat)
   low medium high
V1   0      7    3
V2   2      5    3
V3   5      5    0
V4   5      2    3

这是使用辅助函数的方法。
请注意,对 do.call 的调用是对 的已接受答案中的第二个解决方案,@shreyasgm 对问题的评论中的第二个 link。我刚刚将 cbind 更改为 rbind

fun <- function(DF){
    nms <- names(DF)[-1]
    vals <- unlist(DF[-1])
    lv <- levels(unique(unlist(DF[-1])))
    DF[-1] <- lapply(DF[-1], function(x)  factor(x, levels = lv))
    do.call(rbind, lapply(DF[-1], summary))
}

fun(x.df)
#   high medium low
#V1    3      7   0
#V2    3      5   2
#V3    0      5   5
#V4    3      2   5