I have a quick question. Why does this code give us the mean of all data, not mean value within X??
df1 <- data.frame(X = rep(x = LETTERS[1:2], each = 3), Y = 1:6)
df1<-df1%>%group_by(X)%>%mutate(meanY=mean(Y))
I have a quick question. Why does this code give us the mean of all data, not mean value within X??
df1 <- data.frame(X = rep(x = LETTERS[1:2], each = 3), Y = 1:6)
df1<-df1%>%group_by(X)%>%mutate(meanY=mean(Y))
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