如何计算一列中基于另一列的值之间的差异?

How to calculate the difference between values in one column based on another column?

我正在尝试计算时间点 C1 和 C0 的丰度差异。我想为不同的基因做这个,所以我对基因使用了 group_by,但无法弄清楚如何找到不同时间点的丰度差异。

这是我的尝试之一:


IgH_CDR3_post_challenge_unique_vv <- IgH_CDR3_post_challenge_unique_v %>% 
  group_by(gene ) %>% 
  mutate(increase_in_abundance = (abunance[Timepoint=='C1'])-(abunance[Timepoint=='C0'])) %>% 
  ungroup() 

我的数据看起来像这样:

gene Timepoint abundance
1 C0 5
2 C1 3
1 C1 6
3 C0 2

假设 (!) 每个基因和时间点都有一个条目(与问题中发布的 table 相反),您可以 pivot_wider 您的数据,然后计算每个基因的差异基因。当然,当前的示例对大部分缺失的帮助不大。

df <- data.frame(gene = c(1, 2, 1, 3),
                 Timepoint = c("c0", "c1", "c1", "c0"),
                 abundance = c(5, 3, 6, 2))

library(tidyverse)

df %>%
  pivot_wider(names_from = Timepoint,
              values_from = abundance,
              id_cols = gene) %>%
  mutate(increase_in_abundance = c1 - c0)

# A tibble: 3 x 4
   gene    c0    c1 increase_in_abundance
  <dbl> <dbl> <dbl>                 <dbl>
1     1     5     6                     1
2     2    NA     3                    NA
3     3     2    NA                    NA