Subtracting values group-wise by the average of each group in R

。_饼干妹妹 提交于 2021-02-17 05:19:27

问题


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.


回答1:


Using the library dplyr, you can do:

library(dplyr)
x %>%
  group_by(gene) %>%
  mutate_all(funs(.-mean(.)))

# A tibble: 8 x 2
# Groups:   gene [3]
  gene   value
  <fct>  <dbl>
1 A      1.03 
2 A     -0.267
3 A     -0.767
4 B      1.45 
5 B     -1.45 
6 C      0    
7 C      0.700
8 C     -0.700



回答2:


A data.table method:

require(data.table)

setDT(x)[, `value-group.mean` := mean(value), by = gene][, `value-group.mean` := value - `value-group.mean`]

x
#   gene value value-group.mean
# 1:    A  32.3        1.0333333
# 2:    A  31.0       -0.2666667
# 3:    A  30.5       -0.7666667
# 4:    B  25.0        1.4500000
# 5:    B  22.1       -1.4500000
# 6:    C  20.5        0.0000000
# 7:    C  21.2        0.7000000
# 8:    C  19.8       -0.7000000



回答3:


You can use ave() to achieve it when with base R, and in that case you don't need to apply aggregate intermediately:

x$value_group.mean <- with(x,value-ave(value,gene))

such that

> x
  gene value value_group.mean
1    A  32.3        1.0333333
2    A  31.0       -0.2666667
3    A  30.5       -0.7666667
4    B  25.0        1.4500000
5    B  22.1       -1.4500000
6    C  20.5        0.0000000
7    C  21.2        0.7000000
8    C  19.8       -0.7000000


来源:https://stackoverflow.com/questions/58948236/subtracting-values-group-wise-by-the-average-of-each-group-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!