通过线性回归减少数据框架

时间:2016-01-27 10:08:05

标签: r

我想减少以下数据框:

Subject, Run, Condition, DV
1, 1, 1, 123
1, 1, 2, 456
1, 1, 3, 789
1, 2, 1, 123
1, 2, 2, 456
1, 2, 3, 789
2, 1, 1, 123
2, 1, 2, 456
2, 1, 3, 789
2, 2, 1, 123
2, 2, 2, 456
2, 2, 3, 789

到这个数据框:

Subject, Run, coeff(lm(DV ~ Condition))
1, 1, ???
1, 2, ???
2, 1, ???
2, 2, ???

有关如何处理此事的任何想法?

1 个答案:

答案 0 :(得分:0)

您应该使用do而不是summarize

df %>% group_by(Subject, Run) %>%
  do(lm = lm(DV ~ Condition,data=.)) %>% 
  summarize(Subject=Subject,
            Run=Run,
            Coef=coef(lm)[[2]])