-1
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
,我可以計算由組中的一個R列
aggregate(df$y, list(df$group), mean)
得到平均水平,但我的問題是:如何做這樣的事情:(y_ij-mean_i)
其中mean_i
是平均組i
謝謝。
我猜回答'y - 與(DF,AVE(Y,組))' –
或'用(DF,AVE(Y,組,FUN =功能(X ){x-mean(x)}))'(='ave(df $ y,df $ group,FUN = function(x){x-mean(x)})') – Cath
或'dplyr','' df%>%group_by(group)%>%mutate(mean_y = y - mean(y))' –