如何在数据框中获得该平均值?

时间:2019-08-23 19:22:28

标签: r

我知道我的下一个问题在R中非常基本,但是我是新手! 我有一个带有qPCR信息的数据框。我想做的是用ct平均值作为喷洒,基因型和基因的函数的新列。这是我的数据框,因此您可以理解我的意思:

> d
   gene sampleCode genotype spraying    ct mean.ct
1     1 1-C1-R1-SA        a  without 31.06   31.06
2     1 1-C1-R2-SA        a  without 30.71   31.06
3     1 1-C1-R3-SA        a  without 31.42   31.06
4     1 1-C1-R1-CA        a     with 31.78   31.98
5     1 1-C1-R2-CA        a     with 32.07   31.98
6     1 1-C1-R3-CA        a     with 32.08   31.98
7     1 2-C2-R1-SA        b  without 32.16   32.16
8     1 2-C2-R2-SA        b  without 32.52   32.16
9     1 2-C2-R3-SA        b  without 31.80   32.16
10    1 2-C2-R1-CA        b     with 32.55   32.28
11    1 2-C2-R2-CA        b     with 32.39   32.28
12    1 2-C2-R3-CA        b     with 31.91   32.28
13    2 1-C1-R1-SA        a  without 31.21   31.58
14    2 1-C1-R2-SA        a  without 31.96   31.58
15    2 1-C1-R3-SA        a  without 31.58   31.58
16    2 1-C1-R1-CA        a     with 32.75   32.75
17    2 1-C1-R2-CA        a     with 32.53   32.75
18    2 1-C1-R3-CA        a     with 32.98   32.75
19    2 2-C2-R1-SA        b  without 31.64   31.64
20    2 2-C2-R2-SA        b  without 32.83   31.64
21    2 2-C2-R3-SA        b  without 30.45   31.64
22    2 2-C2-R1-CA        b     with 31.97   32.43
23    2 2-C2-R2-CA        b     with 32.60   32.43
24    2 2-C2-R3-CA        b     with 32.72   32.43

我在excel中创建了“ mean.ct”列,但由于我掌握了很多信息,所以我无法对所有行进行列!有谁知道我可以用一个简单的代码在R中创建这个新列?我以为使用功能“为”和“如果”。但是我不知道怎么做! 任何帮助将不胜感激! 谢谢!

2 个答案:

答案 0 :(得分:3)

您可以使用data.table库。对于大型数据集,这是非常快的。尝试以下代码:

library(data.table)
d[, mean_ct := mean(ct), by = list(spraying, genotype, gene)]

答案 1 :(得分:1)

这是简单的数据转换。对我而言,最简单的方法是使用tidyverse

包裹。

library(tidyverse)

df <- read.table(text = "gene sampleCode genotype spraying    ct mean.ct
1     1 1-C1-R1-SA        a  without 31.06   31.06
2     1 1-C1-R2-SA        a  without 30.71   31.06
3     1 1-C1-R3-SA        a  without 31.42   31.06
4     1 1-C1-R1-CA        a     with 31.78   31.98
5     1 1-C1-R2-CA        a     with 32.07   31.98
6     1 1-C1-R3-CA        a     with 32.08   31.98
7     1 2-C2-R1-SA        b  without 32.16   32.16
8     1 2-C2-R2-SA        b  without 32.52   32.16
9     1 2-C2-R3-SA        b  without 31.80   32.16
10    1 2-C2-R1-CA        b     with 32.55   32.28
11    1 2-C2-R2-CA        b     with 32.39   32.28
12    1 2-C2-R3-CA        b     with 31.91   32.28
13    2 1-C1-R1-SA        a  without 31.21   31.58
14    2 1-C1-R2-SA        a  without 31.96   31.58
15    2 1-C1-R3-SA        a  without 31.58   31.58
16    2 1-C1-R1-CA        a     with 32.75   32.75
17    2 1-C1-R2-CA        a     with 32.53   32.75
18    2 1-C1-R3-CA        a     with 32.98   32.75
19    2 2-C2-R1-SA        b  without 31.64   31.64
20    2 2-C2-R2-SA        b  without 32.83   31.64
21    2 2-C2-R3-SA        b  without 30.45   31.64
22    2 2-C2-R1-CA        b     with 31.97   32.43
23    2 2-C2-R2-CA        b     with 32.60   32.43
24    2 2-C2-R3-CA        b     with 32.72   32.43")


df%>%
  group_by(gene, genotype, spraying)%>%
  mutate(mean.ct2 = mean(ct))%>%
  View

首先group变量,然后mutate变量就可以得到结果。我将其列称为mean.ct2以查看是否可以生成您的值。您可能应该为项目重命名它们。 我希望这有帮助。