我正在分析一些数据,但遇到了困难-我不知道如何使用变量名作为“组”来总结整个数据集。
示例数据:
structure(list(x4 = c(3, 4, 7, 4, 5, 1, 5, 2, 7, 1), x5 = c(2,
4, 4, 4, 5, 3, 6, 1, 7, 1), x6 = c(3, 5, 4, 7, 5, 4, 6, 4, 6,
2), x7 = c(4, 1, 6, 4, 6, 4, 6, 2, 7, 2), x9 = c(5, 5, 4, 5,
6, 3, 7, 5, 6, 1), x10 = c(3, 6, 5, 4, 6, 5, 6, 3, 6, 1), x11 = c(6,
7, 7, 7, 6, 7, 7, 5, 7, 4), x12 = c(6, 7, 6, 7, 6, 4, 6, 6, 7,
5), x14 = c(5, 7, 5, 6, 4, 6, 6, 5, 6, 4), x15 = c(4, 7, 7, 7,
6, 4, 6, 5, 6, 1), x16 = c(4, 7, 7, 7, 6, 5, 7, 3, 6, 4), x17 = c(4,
5, 5, 7, 6, 6, 7, 4, 6, 2), x18 = c(3, 4, 7, 7, 6, 5, 6, 4, 6,
2), x19 = c(5, 7, 5, 7, 6, 6, 6, 3, 6, 1), x22 = c(4, 4, 5, 7,
6, 7, 6, 5, 6, 2), x26 = c(6, 7, 5, 4, 6, 7, 7, 4, 6, 4), x29 = c(4,
7, 2, 7, 6, 4, 7, 3, 6, 1), x33 = c(3, 7, 7, 7, 6, 5, 6, 3, 6,
3), x34 = c(5, 5, 4, 7, 6, 7, 7, 5, 6, 2), x35 = c(4, 4, 7, 7,
5, 7, 6, 4, 6, 2), x36 = c(4, 7, 6, 7, 6, 5, 5, 4, 6, 2), x37 = c(3,
4, 7, 4, 5, 4, 6, 3, 5, 2), x49 = c(4, 7, 7, 7, 6, 5, 5, 6, 6,
3), x50 = c(4, 7, 6, 5, 5, 5, 6, 5, 7, 4)), row.names = c(NA,
-10L), class = "data.frame", .Names = c("x4", "x5", "x6", "x7",
"x9", "x10", "x11", "x12", "x14", "x15", "x16", "x17", "x18",
"x19", "x22", "x26", "x29", "x33", "x34", "x35", "x36", "x37",
"x49", "x50"))
我只想要一些统计信息,例如:
summary <- dados_afc %>%
summarise_all(funs(mean, sd, mode, median))
但是结果是具有一个观察值和许多变量的df。我希望它有5列:varname,mean,sd,mode,niddle,但是我不确定如何做到这一点。有提示吗?
答案 0 :(得分:2)
注意:我不知道从R获取模式的内置方法。请参见此处进行一些讨论: Is there a built-in function for finding the mode?
# From the top answer there:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
要将每个列视为一个组,可以使用tidyr::gather
将“宽”数据转换为长格式,然后使用dplyr::group_by
创建具有自己的摘要计算的组:
library(tidyverse)
summary <- dados_afc %>%
gather(group, value) %>%
group_by(group) %>%
summarise_all(funs(mean, sd, Mode, median))
> summary
# A tibble: 24 x 5
group mean sd Mode median
<chr> <dbl> <dbl> <dbl> <dbl>
1 x10 4.5 1.72 6 5
2 x11 6.3 1.06 7 7
3 x12 6 0.943 6 6
4 x14 5.4 0.966 6 5.5
5 x15 5.3 1.89 7 6
6 x16 5.6 1.51 7 6
7 x17 5.2 1.55 6 5.5
8 x18 5 1.70 6 5.5
9 x19 5.2 1.87 6 6
10 x22 5.2 1.55 6 5.5