So we want to add person-level summaries of a variable to the right of a data-frame?

  1. Create some data
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
suppressMessages(library(data.table))

d <- data.frame(ID = rep(seq(1,5,1), each = 5),
                X  = rnorm(25))
head(d) %>% knitr::kable()
ID X
1 -0.0384933
1 -0.2161823
1 -0.2506520
1 -0.2967449
1 -0.6339499
2 0.5708115
  1. The dplyr way
d %>%
  group_by(ID) %>% 
  mutate(mean_of_X = mean(X)) %>%
  head(.) %>% knitr::kable()
ID X mean_of_X
1 -0.0384933 -0.2872045
1 -0.2161823 -0.2872045
1 -0.2506520 -0.2872045
1 -0.2967449 -0.2872045
1 -0.6339499 -0.2872045
2 0.5708115 0.1678269
  1. The data.table way
DT <- as.data.table(d)
DT[, mean_of_X := mean(X), by = ID]
head(DT) %>% knitr::kable()
ID X mean_of_X
1 -0.0384933 -0.2872045
1 -0.2161823 -0.2872045
1 -0.2506520 -0.2872045
1 -0.2967449 -0.2872045
1 -0.6339499 -0.2872045
2 0.5708115 0.1678269
  1. The base R way
d <- merge(d, aggregate(d$X, by=list(d$ID), FUN=mean), by.x = 'ID', by.y = 'Group.1')
head(d) %>% knitr::kable()
ID X x
1 -0.0384933 -0.2872045
1 -0.2161823 -0.2872045
1 -0.2506520 -0.2872045
1 -0.2967449 -0.2872045
1 -0.6339499 -0.2872045
2 0.5708115 0.1678269