-2
group=c("A","A","B","A","B","C","C","A")
y=c(3,4,5,2,1,4,1,2)
df=data.frame(group,y)

using aggregate, I can get the average by

aggregate(df$y, list(df$group), mean)

But my question is: How to do something like : (y_ij-mean_i) where mean_i is the average for group i

thank you.

Math
  • 1,274
  • 3
  • 14
  • 32

1 Answers1

2

We can use ave

 with(df, y- ave(y, group))
akrun
  • 874,273
  • 37
  • 540
  • 662