You can use ave
to calculate the groupmeans:
df$MeanValue <- with(df, ave(Value, Insertion, Unit, Channel, FUN = mean))
Then calculate the residuals:
df$Residual <- df$Value - df$MeanValue
df
# Insertion Measurement Unit Channel Value MeanValue Residual
#1 1 1 A5 10 9.41 9.403333 0.006666667
#2 1 1 A5 11 9.51 9.470000 0.040000000
#3 1 1 A5 12 10.59 10.850000 -0.260000000
#4 1 1 A5 13 9.45 9.476667 -0.026666667
#5 1 2 A5 10 9.42 9.403333 0.016666667
#6 1 2 A5 11 9.03 9.470000 -0.440000000
#7 1 2 A5 12 10.62 10.850000 -0.230000000
#8 1 2 A5 13 9.39 9.476667 -0.086666667
#9 1 3 A5 10 9.38 9.403333 -0.023333333
#10 1 3 A5 11 9.87 9.470000 0.400000000
#11 1 3 A5 12 11.34 10.850000 0.490000000
#12 1 3 A5 13 9.59 9.476667 0.113333333
#13 2 1 A5 10 12.10 12.100000 0.000000000
#14 2 1 A5 11 11.28 11.280000 0.000000000
#15 2 1 A5 12 12.95 12.950000 0.000000000
Or you could use dplyr
library(dplyr)
df %>% group_by(Insertion, Unit, Channel) %>% mutate(MeanValue = mean(Value), Residual = Value - MeanValue)