-1

I would like to reduce the following data frame:

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

To this data frame:

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

Any ideas on how to approach this?

fladd
  • 19
  • 1
  • 3
    Please have a look at [Minimal Working Examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, please include a way in which we can easily reproduce your data. – David Jan 27 '16 at 10:11
  • @David: Data reproduction is not the main issue here: `read.table(sep = ",", header = TRUE, text = "...")` does the trick. I'm truly wondering where the 0.9 comes from. @fladd: What have you tried already? – krlmlr Jan 27 '16 at 10:25
  • 2
    From such a regression you get two values as coefficients. Why in your desired output is only one column for the coefficients? Please edit your question! – jogo Jan 27 '16 at 10:25
  • @krlmlr, although its not the main issue (on which I totally agree on), it makes it easier for us to try solutions (if we don't find an immediate answer) and/or easier to grasp the problem. – David Jan 27 '16 at 10:27
  • 2
    The classic solution for this is a split-apply-combine function. How those work has been explained on Stack Overflow numerous times. – Roland Jan 27 '16 at 10:37
  • @David, @krlmlr, @jogo, @Roland: Thanks for the comments so far. The number 0.9 is a guess only, it is an example. I want the coefficient of the linear regression of the three values from DV for each Subject * Run combination. I tried the following, but it did not work: `data %>% group_by(Subject, Run) %>% summarise(Coef = coef(lm(DV ~ Condition)))`. It throws an error telling me that the expression is not correct. – fladd Jan 27 '16 at 14:48

1 Answers1

0

You should use do rather than summarize:

df %>% group_by(Subject, Run) %>%
  do(lm = lm(DV ~ Condition,data=.)) %>% 
  summarize(Subject=Subject,
            Run=Run,
            Coef=coef(lm)[[2]])
scoa
  • 19,359
  • 5
  • 65
  • 80