I have the below example data and I am trying to create a new column with mutate()
that takes each dist
value and subtracts it from the mean of the entire dist
column. To take it one step further, I would like the mean to be based on the qtr
variable. For example, each dist
value that corresponds to qtr == 1
is subtracted from the mean of dist
for qtr == 1
only and so on. Hope that makes sense.
library(tidyverse)
set.seed(10)
rd <- rep(rep(c(1:20), each = 4))
qtr <- rep(c(1:4), times = 20)
dist <- round(rnorm(n = 80, mean = 3000, sd = 500))
df <- data.frame(rd, qtr, dist)
I can get to here, but I'm not sure how I can account for the qtr
variable. At the moment, the below code is taking the mean of dist
irrespective of the value for qtr
.
dat %>%
mutate(new_column = (dist - mean(dist)))