我一直在寻找如何使用stargazer包创建一个按R中的分类变量分组的汇总统计表。
基本上,我想显示彼此相邻的两组(控制和治疗)的方法,并另外计算两组之间的差异。
每当我尝试使用stargazer创建表时,它会为每个分类变量创建两个表。
我使用mtcars数据集创建了一个示例。假设变量'am'是'是分类变量:
attach(mtcars)
library(dplyr)
data = mtcars
auto1 = data %>%
filter(am == 1) %>%
dplyr::select(mpg,disp,hp)
manu1 = data %>%
filter(am == 0) %>%
dplyr::select(mpg,disp,hp)
stargazer(auto1,manu1, type = "html", out = "summary.html",summary.stat = c("mean"), summary = TRUE)`
由于这没有达到预期的效果,我手动创建了摘要表,并在stargazer中指定了FALSE的摘要以获取HTML表格:
auto = data %>%
filter(am == 1) %>%
summarize_each(funs(mean)) %>%
melt(id.vars="am")
manu = data %>%
filter(am == 0) %>%
summarize_each(funs(mean)) %>%
melt(id.vars = "am")
end = dplyr::select(data.frame(auto,manu),-c(am,am.1,variable.1))
end$diff = end$value.1 - end$value
names(end) = c("Variable","Automatic","Manual","Difference")
stargazer(end, type = "html", out = "summary.html",summary.stat = c("mean"), summary = FALSE)
这可能不是创建所需汇总统计表的简洁方法,但我自己无法找到更好的方法。任何建议如何与观星者或其他包合作?
答案 0 :(得分:2)
不完全确定您想要的输出是什么,但这有帮助吗?
mtcars %>%
group_by(am) %>%
summarise(mpg = mean(mpg), disp = mean(disp), hp = mean(hp)) %>%
gather(key = "variable","value",mpg,disp,hp) %>%
spread(am,value) %>%
group_by(variable) %>%
mutate(difference = `1`-`0`)
## Source: local data frame [3 x 4]
## Groups: variable [3]
##
## variable `0` `1` difference
## <chr> <dbl> <dbl> <dbl>
## 1 disp 290.37895 143.53077 -146.848178
## 2 hp 160.26316 126.84615 -33.417004
## 3 mpg 17.14737 24.39231 7.244939