我有一些来自850种蛋白质的蛋白质表达数据,我想将数据标准化为参考蛋白质。这是纠正技术错误的好方法。我是R的新手,只是想出一个整洁的数据集。但是当我搜索规范化时 - 它主要是缩放数据。我找不到与数据集中的数据点进行比率的好方法。所以我有以下,其中type = D或T,pt.num = 1-8,并且在612.9 kb文件中有859个GeneID和9952个元素。
> head(df10g)
GeneID type pt.num value
1 A2M D 1 8876.5
2 ABL1 D 1 2120.8
3 ACP1 D 1 1266.6
4 ACP5 D 1 67797.6
5 ACVRL1 D 1 650.1
6 ACY1 D 1 6264.8
318 IGF2R D 1 6294.8
我想为每个pt.num.type标准化为IGF2R。但我无法弄清楚它的语义。我想要这种类型的功能
Norm.ig2Fr=GeneID.type.pt.num(value)/IG2FR.type.pt.num(value)
Norm.ig2fr=ASM.D.1 (value)/IG2FR.D.1 (value)
Norm.ig2fr=8876.5/6294.8
所需的输出是
GeneID type pt.num value Norm.ig2fr log2Norm.ig2fr
1 A2M D 1 8876.5 1.41 0.49
2 ABL1 D 1 2120.8
3 ACP1 D 1 1266.6
4 ACP5 D 1 67797.6
我想我可以使用mutate或ddply转换,但是我错过了将比率的分母固定到相同GeneID值但改变pt.num和type的东西。
df11 <- ddply(df10g, .(pt.num), transform, Norm.ig2b=value/IGF2R)
或
df10.igf2r<- mutate(df10t, .(type, pt.num), Norm.ig2fr=value/IG2FR)
dput(df10g)
structure(list(GeneID = structure(c(1L, 2L, 3L, 4L, 6L, 7L), .Label = c("A2M",
"ABL1", "ACP1", "ACP5", "Activated Protein C", "ACVRL1", "ACY1"),class = "factor"), type = c("D", "D", "D", "D", "D",
"D"), pt.num = c("1", "1", "1", "1", "1", "1"), value = c(8876.5,
2120.8, 1266.6, 67797.6, 650.1, 6264.8)), .Names = c("GeneID",
"type", "pt.num", "value"), row.names = c(NA, 6L), class = "data.frame")
任何建议或见解将不胜感激。谢谢你的帮助!
答案 0 :(得分:1)
我认为这可能就是你的意思。我无法找到plyr
的解决方案。但是,我想对dplyr
提出建议。我在这里创建了一个示例数据来演示代码。我想您想使用type
对pt.num
和group_by()
进行分组。然后,您想要在mutate()
中进行规范化。 value[GeneID == "IGF2R"]
指定每个组中IGF2R
的值。例如,对于D-1组,value[GeneID == "IGF2R"]
是1281.000,对于T-1组,value[GeneID == "IGF2R"]
是1561.364。使用这些值,R对每个组进行标准化。
set.seed(111)
mydf <- data.frame(GeneID = rep(c("A2M", "ABL1", "ACP1", "ACP5",
"ACVRL1", "ACY1", "IGF2R"), times = 2),
type = rep(c("D", "T"), each = 7),
pt.num = 1,
value = runif(14, 1200, 8800),
stringsAsFactors = FALSE)
# GeneID type pt.num value
#1 A2M D 1 5706.658
#2 ABL1 D 1 6721.257
#3 ACP1 D 1 4015.207
#4 ACP5 D 1 5113.421
#5 ACVRL1 D 1 4070.240
#6 ACY1 D 1 4379.364
#7 IGF2R D 1 1281.000
#8 A2M T 1 5245.444
#9 ABL1 T 1 4484.421
#10 ACP1 T 1 1911.980
#11 ACP5 T 1 5423.927
#12 ACVRL1 T 1 5685.737
#13 ACY1 T 1 1710.273
#14 IGF2R T 1 1561.364
library(dplyr)
group_by(mydf, type, pt.num) %>%
mutate(out = value / value[GeneID == "IGF2R"])
# GeneID type pt.num value out
#1 A2M D 1 5706.658 4.454847
#2 ABL1 D 1 6721.257 5.246884
#3 ACP1 D 1 4015.207 3.134433
#4 ACP5 D 1 5113.421 3.991743
#5 ACVRL1 D 1 4070.240 3.177394
#6 ACY1 D 1 4379.364 3.418708
#7 IGF2R D 1 1281.000 1.000000
#8 A2M T 1 5245.444 3.359527
#9 ABL1 T 1 4484.421 2.872118
#10 ACP1 T 1 1911.980 1.224557
#11 ACP5 T 1 5423.927 3.473840
#12 ACVRL1 T 1 5685.737 3.641520
#13 ACY1 T 1 1710.273 1.095371
#14 IGF2R T 1 1561.364 1.000000
在data.table
中应用相同的步骤,以下代码也正常工作。
library(data.table)
foo <- setDT(mydf)[, out := value / value[GeneID == "IGF2R"], by = list(type, pt.num)]
print(foo)
# GeneID type pt.num value out
#1: A2M D 1 5706.658 4.454847
#2: ABL1 D 1 6721.257 5.246884
#3: ACP1 D 1 4015.207 3.134433
#4: ACP5 D 1 5113.421 3.991743
#5: ACVRL1 D 1 4070.240 3.177394
#6: ACY1 D 1 4379.364 3.418708
#7: IGF2R D 1 1281.000 1.000000
#8: A2M T 1 5245.444 3.359527
#9: ABL1 T 1 4484.421 2.872118
#10: ACP1 T 1 1911.980 1.224557
#11: ACP5 T 1 5423.927 3.473840
#12: ACVRL1 T 1 5685.737 3.641520
#13: ACY1 T 1 1710.273 1.095371
#14: IGF2R T 1 1561.364 1.000000