【问题标题】:Calculate the levels of mutiple variables and return tabular result计算多个变量的水平并返回表格结果
【发布时间】:2018-04-22 12:12:43
【问题描述】:

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

   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。

【问题讨论】:

标签: r dataframe tabular summary


【解决方案1】:

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

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

想法是使用sapply 遍历这些变量中的每一个,将变量转换为具有所需水平的因子,然后在转换后的变量上调用 table。

返回

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

【讨论】:

  • 谢谢。由于变量已经是因子,所以我不需要 factor(...) 函数。
【解决方案2】:

这是一种使用辅助函数的方法。
请注意,对do.call 的调用是this question 接受答案中的第二个解决方案,是@shreyasgm 对问题的评论中的第二个链接。我刚刚将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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多