Example:
So lets say I have this data frame.
x = data.frame(factor = as.factor(c('a','a','b','b','c','c')),value1 = c(1,3,2,4,5,3), value2 = c(7,9,3,4,9,3))
factor value1 value2
1 a 1 7
2 a 3 9
3 b 2 3
4 b 4 4
5 c 5 9
6 c 3 3
I know how to get the mean per factor, I use this method:
aggregate(x[,c(2,3)], list(x$factor), mean, na.rm = T )
This give me the following output:
Group.1 value1 value2
1 a 2 8.0
2 b 3 3.5
3 c 4 6.0
How do I now go about subtracting from each value in the original dataframe the corresponding mean of its factor. The actual dataset I am using is big so need to have a nice way, I have managed to do it but I used complicated for loops.
So the output that I want would be:
factor value1 value2
1 a -1 -1.0
2 a 1 1.0
3 b -1 -0.5
4 b 1 0.5
5 c 1 3.0
6 c -1 -3.0
Any help would be great. Thanks.
A
dplyr
solutionOutput