我正在为热图准备数据,我想绘制相对于最高值的更改。我想比较模式而不是每个node --version
v11.4.0
npm --version
6.4.1
的绝对丰度,并且还要将热图的比例限制为0到100%。
这是我的数据
id
我尝试添加一列相对值,该列将每个head(kallisto_melt,14)
id protein_name variable value relative_abundance
1: BIJBGGEO_00001 hypothetical protein tpm_A1 0.0000000 NA
2: BIJBGGEO_00001 hypothetical protein tpm_A2 0.0000000 NA
3: BIJBGGEO_00001 hypothetical protein tpm_A3 0.0000000 NA
4: BIJBGGEO_00001 hypothetical protein tpm_A4 0.0000000 NA
5: BIJBGGEO_00001 hypothetical protein tpm_A5 0.0000000 NA
6: BIJBGGEO_00001 hypothetical protein tpm_A6 0.0000000 NA
7: BIJBGGEO_00001 hypothetical protein tpm_A7 0.0000000 NA
8: BIJBGGEO_00002 hypothetical protein tpm_A1 0.0000000 NA
9: BIJBGGEO_00002 hypothetical protein tpm_A2 0.0000000 NA
10: BIJBGGEO_00002 hypothetical protein tpm_A3 0.0000000 NA
11: BIJBGGEO_00002 hypothetical protein tpm_A4 0.0703664 NA
12: BIJBGGEO_00002 hypothetical protein tpm_A5 0.0000000 NA
13: BIJBGGEO_00002 hypothetical protein tpm_A6 0.0000000 NA
14: BIJBGGEO_00002 hypothetical protein tpm_A7 0.0863996 NA
的最高value
设置为100%,其他值也相应地设置。我可以想象所有零都将导致NA(前7行),但是对于第二个id
,我期望是这样的:
id
我修改了曾经在这里R how to calculate relative values based on a long format data.frame column?要求的代码
它看起来像这样:
id protein_name variable value relative_abundance
1: BIJBGGEO_00001 hypothetical protein tpm_A1 0.0000000 NA
2: BIJBGGEO_00001 hypothetical protein tpm_A2 0.0000000 NA
3: BIJBGGEO_00001 hypothetical protein tpm_A3 0.0000000 NA
4: BIJBGGEO_00001 hypothetical protein tpm_A4 0.0000000 NA
5: BIJBGGEO_00001 hypothetical protein tpm_A5 0.0000000 NA
6: BIJBGGEO_00001 hypothetical protein tpm_A6 0.0000000 NA
7: BIJBGGEO_00001 hypothetical protein tpm_A7 0.0000000 NA
8: BIJBGGEO_00002 hypothetical protein tpm_A1 0.0000000 0
9: BIJBGGEO_00002 hypothetical protein tpm_A2 0.0000000 0
10: BIJBGGEO_00002 hypothetical protein tpm_A3 0.0000000 0
11: BIJBGGEO_00002 hypothetical protein tpm_A4 0.0703664 "somewhere about 81"
12: BIJBGGEO_00002 hypothetical protein tpm_A5 0.0000000 0
13: BIJBGGEO_00002 hypothetical protein tpm_A6 0.0000000 0
14: BIJBGGEO_00002 hypothetical protein tpm_A7 0.0863996 100
我在做什么错了?
答案 0 :(得分:2)
使用此代码:-您将能够找到它。
library(dplyr)
df1 <- df %>%
group_by(id,protein_name) %>%
mutate(relative_abundance = value/max(value)*100)
df1[is.na(df1)] <- 0
数据:-
df<- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("BIJBGGEO_00001", "BIJBGGEO_00002"
), class = "factor"), protein_name = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "hypothetical protein", class = "factor"),
variable = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L), .Label = c("tpm_A1", "tpm_A2", "tpm_A3",
"tpm_A4", "tpm_A5", "tpm_A6", "tpm_A7"), class = "factor"),
value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0703664, 0, 0,
0.0863996), relative_abundance = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-14L))
答案 1 :(得分:1)
有了data.table
,我们可以做到
# setDT(kallisto_melt)
kallisto_melt[, relative_abundance := value / max(value) * 100, by = id]
kallisto_melt[is.na(relative_abundance), relative_abundance := 0]
kallisto_melt
# id protein_name variable value #relative_abundance
# 1: BIJBGGEO_00001 hypothetical protein tpm_A1 0.0000000 0.00000
# 2: BIJBGGEO_00001 hypothetical protein tpm_A2 0.0000000 0.00000
# 3: BIJBGGEO_00001 hypothetical protein tpm_A3 0.0000000 0.00000
# 4: BIJBGGEO_00001 hypothetical protein tpm_A4 0.0000000 0.00000
# 5: BIJBGGEO_00001 hypothetical protein tpm_A5 0.0000000 0.00000
# 6: BIJBGGEO_00001 hypothetical protein tpm_A6 0.0000000 0.00000
# 7: BIJBGGEO_00001 hypothetical protein tpm_A7 0.0000000 0.00000
# 8: BIJBGGEO_00002 hypothetical protein tpm_A1 0.0000000 0.00000
# 9: BIJBGGEO_00002 hypothetical protein tpm_A2 0.0000000 0.00000
#10: BIJBGGEO_00002 hypothetical protein tpm_A3 0.0000000 0.00000
#11: BIJBGGEO_00002 hypothetical protein tpm_A4 0.0703664 81.44297
#12: BIJBGGEO_00002 hypothetical protein tpm_A5 0.0000000 0.00000
#13: BIJBGGEO_00002 hypothetical protein tpm_A6 0.0000000 0.00000
#14: BIJBGGEO_00002 hypothetical protein tpm_A7 0.0863996 100.00000