I have a dataframe that looks like this:
example <- data.frame(
ID_A = c(1101, 1101, 1102, 1102, 1103, 1103),
ID_B = c(2101, 2101, 2102, 2102, 2103, 2103),
A = c(1,2,3,1,2,3),
B = c(2,2,3,2,2,3),
C = c(3,2,3,3,2,3)
)
For the selected variables of interest, I want to apply the following steps. I am essentially creating 3 new columns from the original column:
example$C.c <- scale(example$C, scale = FALSE)
example <- example %>%
group_by(ID_A) %>%
dplyr::mutate(., C.cb = mean(C.c, na.rm = TRUE)) %>%
ungroup() %>%
group_by(ID_A) %>%
dplyr::mutate(., C.cw = C.c - C.cb) %>%
ungroup()
The variables of interest here are var_A
, var_B
and var_C
. This is the desired resulting dataframe:
example_solution <- data.frame(
ID_A = c(1101, 1101, 1102, 1102, 1103, 1103),
ID_B = c(2101, 2101, 2102, 2102, 2103, 2103),
A = c(1,2,3,1,2,3),
B = c(2,2,3,2,2,3),
C = c(3,2,3,3,2,3),
A.c = c(-1, 0, 1, -1, 0, 1),
A.cb = c(-0.5, -0.5, 0.0, 0.0, 0.5, 0.5),
A.cw = c(-0.5, 0.5, 1.0, -1.0, -0.5, 0.5),
B.c = c(-0.3333333, -0.3333333, 0.6666667, -0.3333333, -0.3333333, 0.6666667),
B.cb = c(-0.3333333, -0.3333333, 0.1666667, 0.1666667, 0.1666667, 0.1666667),
B.cw = c(0.0, 0.0, 0.5, -0.5, -0.5, 0.5),
C.c = c(0.3333333, -0.6666667, 0.3333333, 0.3333333, -0.6666667, 0.3333333),
C.cb = c(-0.1666667, -0.1666667, 0.3333333, 0.3333333, -0.1666667, -0.1666667),
C.cw = c(0.5, -0.5, 0.0, 0.0, -0.5, 0.5)
)
How can I do this efficiently? Thank you!
Edit: Removed var_ in front of variables A
, B
and C
.