I am trying to subtract group means from each group's values.
For example:
> x <- data.frame('gene' = c('A','A','A','B','B','C','C','C'),'value' = c(32.3,31,30.5,25,22.1,20.5,21.2,19.8))
> x
gene value
1 A 32.3
2 A 31.0
3 A 30.5
4 B 25.0
5 B 22.1
6 C 20.5
7 C 21.2
8 C 19.8
I can find the group means:
> aggregate(x[,2],list(x$gene),mean)
Group.1 x
1 A 31.26667
2 B 23.55000
3 C 20.50000
How do I subtract the "value" in x by the corresponding group mean? My desire result is as follow:
gene value-group.mean
1 A 1.03333
2 A -0.26667
3 A -0.76667
4 B 1.45
5 B -1.45
6 C 0
7 C 0.7
8 C -0.7
How can I do it in R?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…