关于使用dplyr的组内平均值

我有一个快速的问题。为什么这段代码给我们所有数据的平均值,而不是X内的平均值?

   df1 <- data.frame(X = rep(x = LETTERS[1:2], each = 3), Y = 1:6)
   df1<-df1%>%group_by(X)%>%mutate(meanY=mean(Y))
评论
melissashu
melissashu

The reason is that mutate loaded is from plyr masking the dplyr::mutate. Either do this on a fresh R session without loading the plyr or use :: to specify the package name

library(dplyr)
df1%>%
    group_by(X)%>%
    dplyr::mutate(meanY=mean(Y))
# A tibble: 6 x 3
# Groups:   X [2]
#  X         Y meanY
#  <fct> <int> <dbl>
#1 A         1     2
#2 A         2     2
#3 A         3     2
#4 B         4     5
#5 B         5     5
#6 B         6     5

OP's case can be replicated if we use plyr::mutate

df1%>%
     group_by(X)%>%
     plyr::mutate(meanY=mean(Y))
# A tibble: 6 x 3
# Groups:   X [2]
#  X         Y meanY
#  <fct> <int> <dbl>
#1 A         1   3.5
#2 A         2   3.5
#3 A         3   3.5
#4 B         4   3.5
#5 B         5   3.5
#6 B         6   3.5
点赞
评论