我的数据框如下:
Variable 1 | Variable 2 | Variable 3 | Outcome Variable
---------- | ---------- | ---------- | ----------------
Factor | Factor | Factor | Outcome
为几千行,15个可变列和1个输出列。我想以下面的长格式总结表格(最好使用plyr):
Variable 1 | Variable 2 | Variable 3 | Outcome Variable
---------- | ---------- | ---------- | ----------------
Factor 1 | Factor 1 | Factor 1 | Average Outcome
Factor 1 | Factor 1 | Factor 2 | Average Outcome
Factor 1 | Factor 2 | Factor 1 | Average Outcome
Factor 1 | Factor 2 | Factor 2 | Average Outcome
用于不同的变量组合。最简单的方法是什么?
答案 0 :(得分:0)
我们可以使用dplyr
library(dplyr)
df1 %>%
group_by(variable1, variable2, variable3) %>%
summarise(OutcomeVariable = mean(OutcomeVariable))
或base R
aggregate(OutcomeVariable ~., df1, FUN = mean)