1

Is it possible to add group aggregates directly to the long format original data frame in dplyr?

Until now, I did it with two steps: aggregate & merge. Is it possible within dplyr and without the merging step?

library(dplyr)
df <- data.frame(x=rnorm(15), A=factor(rep(1:3, each=5)))
df2 <- df %>% group_by(A) %>% summarise(x_mean=mean(x))
df3 <- merge(df, df2, by="A")

Which results in:

> df3
   A      x x_mean
1  1  0.635 -0.077
2  1 -1.059 -0.077
3  1  0.408 -0.077
4  1  1.404 -0.077
5  1 -1.774 -0.077
6  2 -0.419 -0.256
7  2  0.417 -0.256
8  2 -0.443 -0.256
9  2 -0.431 -0.256
10 2 -0.403 -0.256
11 3  0.239  0.767
12 3  1.060  0.767
13 3  1.633  0.767
14 3  0.265  0.767
15 3  0.640  0.767
Felix S
  • 1,769
  • 2
  • 13
  • 17

1 Answers1

6

You want mutate not summarise I think:

df %>% group_by(A) %>% mutate(x_mean=mean(x))

As a side-note, this is one of those circumstances when the base R solution is incredibly simple:

ave(df$x,df$A)
thelatemail
  • 91,185
  • 12
  • 128
  • 188