根据其他 3 列对某一列中的值求和时,值不正确

发布于 2025-01-15 09:46:31 字数 1277 浏览 3 评论 0原文

我正在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-01-22 09:46:31

目前,sum(rel_abund >= 0) 正在对 >= 0 测试的 TRUE 值求和,将每个值计为 1,因此有效地只是计数。要对值 >= 0 的值求和,请尝试 sum(rel_abund[rel_abund >= 0], na.rm = TRUE)

dat %>%
  group_by(OTU, Genotype) %>%
  summarize(Summed_rel_abund = sum(rel_abund[rel_abund >= 0], na.rm = TRUE)) 

#> # A tibble: 4 x 3
#> # Groups:   OTU [4]
#>   OTU   Genotype  Summed_rel_abund
#>   <chr> <chr>                <dbl>
#> 1 ASV2  genotype1            0.280
#> 2 ASV3  genotype4            0.394
#> 3 ASV4  genotype2            0.966
#> 4 ASV7  genotype1            0.505

创建于 2022 年 3 月 18 日,由 < a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex 包 (v2.0.1)

Currently, sum(rel_abund >= 0) is summing the TRUE values of the >= 0 test, counting each as 1, therefore effectively just counting. To sum the values where the value >= 0 try sum(rel_abund[rel_abund >= 0], na.rm = TRUE):

dat %>%
  group_by(OTU, Genotype) %>%
  summarize(Summed_rel_abund = sum(rel_abund[rel_abund >= 0], na.rm = TRUE)) 

#> # A tibble: 4 x 3
#> # Groups:   OTU [4]
#>   OTU   Genotype  Summed_rel_abund
#>   <chr> <chr>                <dbl>
#> 1 ASV2  genotype1            0.280
#> 2 ASV3  genotype4            0.394
#> 3 ASV4  genotype2            0.966
#> 4 ASV7  genotype1            0.505

Created on 2022-03-18 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文