根据其他 3 列对某一列中的值求和时,值不正确
我正在处理 phyloseq 对象的输出文件。我计算了 rel.abundance 并提取了我感兴趣的列,如下所示: 这就是我计算 rel_abund
dat <- read_excel("selected_data.xlsx")%>% group_by(OTU)%>% mutate(rel_abund = Abundance/sum(Abundance))
dat
OTU 丰度 SampleID 基因型 rel_abund 的 方法 ASV2 4988 P35 基因型1 0.2801617614 ASV4 3894 1P-GH-R2 基因型2 0.9660133962 ASV7 3681 P53 基因型1 0.5047305636 ASV3 2149 P16 genotype4 0.3943842907
然后我需要计算每个基因型中每个 ASV 的相对丰度总和。每个基因型由 1-5 个样本表示,根据样本中的出现情况,我有 2,464 行的 44 个 ASV
为了显示每个 ASV 及其跨基因型(包括样本 ID)的 rel_abund,我
dat %>%
count(OTU, SampleID, rel_abund, Genotype) %>%
pivot_wider(names_from = SampleID, values_from = n)
在
OTU rel_abund Genotype 1P-R1 1P- 中尝试了此结果R2 P1 ...... ASV1 0.0000000000 基因型11 1 1 NA NA NA NA NA
ASV1 0.0000000000 genotype2 NA NA 1 NA NA NA NA
然后,
dat %>%
group_by(OTU, Genotype) %>%
summarize(Summed_rel_abund = sum(rel_abund >= 0, na.rm = TRUE))
给出:
OTU Genotype Summed_rel_abund ASV1 基因型1 1
ASV1 基因型3 3
ASV1 基因型2 1
ASV1 基因型5 3
ASV1 基因型6 2
ASV10 基因型7 5
ASV10 genotype8 5
我不知道总和是整数,而总和值是小数。我对最后一步有疑问,需要更正! 谢谢
I am working on the output file from phyloseq object. I calculated the rel.abundance and extracted the columns I am interested in as follows:
this is how I calculate the rel_abund
dat <- read_excel("selected_data.xlsx")%>% group_by(OTU)%>% mutate(rel_abund = Abundance/sum(Abundance))
dat
OTU Abundance SampleID Genotype rel_abund
ASV2 4988 P35 genotype1 0.2801617614
ASV4 3894 1P-GH-R2 genotype2 0.9660133962
ASV7 3681 P53 genotype1 0.5047305636
ASV3 2149 P16 genotype4 0.3943842907
Then I need to calculate the summed relative abundance of each ASV in each genotype. Each genotype is represented by 1-5 samples, and I have 44 ASVs of 2,464 rows according to their occurrences in samples
To display each ASV along with its rel_abund across genotypes including sampleIDs, I tried this
dat %>%
count(OTU, SampleID, rel_abund, Genotype) %>%
pivot_wider(names_from = SampleID, values_from = n)
results in
OTU rel_abund Genotype 1P-R1 1P-R2 P1 .......
ASV1 0.0000000000 genotype11 1 1 NA NA NA NA NA
ASV1 0.0000000000 genotype2 NA NA 1 NA NA NA NA
Then,
dat %>%
group_by(OTU, Genotype) %>%
summarize(Summed_rel_abund = sum(rel_abund >= 0, na.rm = TRUE))
gives:
OTU Genotype Summed_rel_abund
ASV1 genotype1 1
ASV1 genotype3 3
ASV1 genotype2 1
ASV1 genotype5 3
ASV1 genotype6 2
ASV10 genotype7 5
ASV10 genotype8 5
I do not how the sum is integers and the summed values are decimal fractions. I doubt the last step and I need a correction, please!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,
sum(rel_abund >= 0)
正在对>= 0
测试的TRUE
值求和,将每个值计为 1,因此有效地只是计数。要对值 >= 0 的值求和,请尝试sum(rel_abund[rel_abund >= 0], na.rm = TRUE)
:创建于 2022 年 3 月 18 日,由 < a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex 包 (v2.0.1)
Currently,
sum(rel_abund >= 0)
is summing theTRUE
values of the>= 0
test, counting each as 1, therefore effectively just counting. To sum the values where the value >= 0 trysum(rel_abund[rel_abund >= 0], na.rm = TRUE)
:Created on 2022-03-18 by the reprex package (v2.0.1)