如何在" long"中总结数据框?格式由多列?

时间:2017-05-19 13:53:45

标签: r plyr

我的数据框如下:

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

用于不同的变量组合。最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

我们可以使用dplyr

library(dplyr)
df1 %>%
    group_by(variable1, variable2, variable3) %>%
    summarise(OutcomeVariable = mean(OutcomeVariable))

base R

aggregate(OutcomeVariable ~., df1, FUN = mean)