我知道这个问题非常基本,但是我不知道如何解决。我想从下一个小标题中取平均值:
TRAT GEN REPBIO REPTEC ct
1 CONTROL LOX1 1 1 30.43434
2 CONTROL LOX1 1 2 30.11876
3 CONTROL LOX1 1 3 30.94279
4 CONTROL LOX1 2 1 30.29191
5 CONTROL LOX1 2 2 32.01084
6 CONTROL LOX1 2 3 33.81738
7 CONTROL LOX1 3 1 33.21275
8 CONTROL LOX1 3 2 33.43539
9 CONTROL LOX1 3 3 33.07273
10 DM LOX1 1 1 NA
11 DM LOX1 1 2 33.34657
12 DM LOX1 1 3 32.00453
13 DM LOX1 2 1 36.88890
我想要得到的平均值按照TRAT,GEN和REPBIO分组。我使用dplyr软件包做到了,但是当存在NA的值时,我遇到了问题。例如
d %>% group_by(GEN,TRAT,REPBIO) %>% mutate(mean.ct=mean(ct)) %>% print(n=15)
我明白了
TRAT GEN REPBIO REPTEC ct mean.ct
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 CONTROL LOX1 1 1 30.4 30.5
2 CONTROL LOX1 1 2 30.1 30.5
3 CONTROL LOX1 1 3 30.9 30.5
4 CONTROL LOX1 2 1 30.3 32.0
5 CONTROL LOX1 2 2 32.0 32.0
6 CONTROL LOX1 2 3 33.8 32.0
7 CONTROL LOX1 3 1 33.2 33.2
8 CONTROL LOX1 3 2 33.4 33.2
9 CONTROL LOX1 3 3 33.1 33.2
10 DM LOX1 1 1 NA NA
11 DM LOX1 1 2 33.3 NA
12 DM LOX1 1 3 32.0 NA
13 DM LOX1 2 1 36.9 36.4
14 DM LOX1 2 2 37.1 36.4
15 DM LOX1 2 3 35.2 36.4
在第10行中有一个NA,当我编写代码时就意味着它获得了REPTEC的所有NA。我想要的是将其传递到下一行并进行平均。在此示例中,将是33.34657和32.00453之间的平均值(即32.6755)。所以我想要这种格式:
TRAT GEN REPBIO REPTEC ct mean.ct
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 CONTROL LOX1 1 1 30.4 30.5
2 CONTROL LOX1 1 2 30.1 30.5
3 CONTROL LOX1 1 3 30.9 30.5
4 CONTROL LOX1 2 1 30.3 32.0
5 CONTROL LOX1 2 2 32.0 32.0
6 CONTROL LOX1 2 3 33.8 32.0
7 CONTROL LOX1 3 1 33.2 33.2
8 CONTROL LOX1 3 2 33.4 33.2
9 CONTROL LOX1 3 3 33.1 33.2
10 DM LOX1 1 1 NA **32.6755**
11 DM LOX1 1 2 33.3 **32.6755**
12 DM LOX1 1 3 32.0 **32.6755**
13 DM LOX1 2 1 36.9 36.4
14 DM LOX1 2 2 37.1 36.4
15 DM LOX1 2 3 35.2 36.4
有人知道我要怎么做吗?谢谢!