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)
使用aggregate
,我可以通过
aggregate(df$y, list(df$group), mean)
但我的问题是:如何做:(y_ij-mean_i)
其中mean_i
是群组i
答案 0 :(得分:2)
我们可以使用ave
with(df, y- ave(y, group))