在列 R 内的组之间应用差异和均值

Apply difference and mean between groups within a column R

我有一个这样的数据框:

df = data.frame("subjectID" = c("S1","S2","S2","S1","S1","S2","S2","S1","S1","S2","S1","S2"), "treatment" = c("none","none","none","none","drug1","drug1","drug1","drug1","drug2","drug2","drug2","drug2"), "protein" = c("proteinA","proteinA","proteinB","proteinB","proteinA","proteinA","proteinB","proteinB","proteinA","proteinA","proteinB","proteinB"), "value"= c(5.3,4.3,4.5,2.3,6.5,5.4,1.2,3.2,2.3,4.5,6.5,3.4))

   subjectID treatment  protein value
1         S1      none proteinA   5.3
2         S2      none proteinA   4.3
3         S2      none proteinB   4.5
4         S1      none proteinB   2.3
5         S1     drug1 proteinA   6.5
6         S2     drug1 proteinA   5.4
7         S2     drug1 proteinB   1.2
8         S1     drug1 proteinB   3.2
9         S1     drug2 proteinA   2.3
10        S2     drug2 proteinA   4.5
11        S1     drug2 proteinB   6.5
12        S2     drug2 proteinB   3.4

我必须对此数据框进行以下计算:

  1. 找出每个受试者的每种蛋白质在 treatment = "drug1" 和 treatment = "none" 之间的价值差异。

所以基本上对于单个计算它将是:

diff = df$value[df$subjectID == "S1" & df$treatment == "drug1" & df$protein == "proteinA"] - df$value[df$subjectID == "S1" & df$treatment == "none" & df$protein == "proteinA"] 
diff 
> 1.2 

在上面的示例中,值 6.5 - 5.3 给出了 proteinA 药物处理和未处理样品之间的差异。我同样对 S2 和 proteinA,S1/proteinB 和 S2/proteinB.

重复这个
  1. 找出受试者之间的平均差异。

我的原始数据有 5 个不同的受试者、10 种不同的治疗方法(包括治疗 ==“none”)和 100 种蛋白质,我不可能对每个分组都手动执行此操作。我将不得不计算每种药物治疗和未治疗之间的平均差异(9 种不同的药物治疗与 none 治疗)。

期望的输出可能是这样的:

 resdf
   protein drug1_mean_diff drug2_mean_diff
1 proteinA             1.15            -1.4
2 proteinB             -1.2            1.55

我最终应该有 100 个蛋白质(行)和 9 个平均差(列)

希望这是清楚的。

谢谢!

不知何故,我无法重现问题中显示的预期输出。但是,我认为这段代码应该给出所需的答案。但我可能弄错了或误解了某些东西。所以请在使用代码前检查:

  df = data.frame(subjectID = c("S1","S2","S2","S1","S1","S2","S2","S1","S1","S2","S1","S2"), 
                  treatment = c("none","none","none","none","drug1","drug1","drug1","drug1","drug2","drug2","drug2","drug2"),
                  protein = c("proteinA","proteinA","proteinB","proteinB","proteinA","proteinA","proteinB","proteinB","proteinA","proteinA","proteinB","proteinB"),
                  value = c(5.3,4.3,4.5,2.3,6.5,5.4,1.2,3.2,2.3,4.5,6.5,3.4))
  
  
  df %>% 
     filter(treatment != "none") %>% 
     left_join(df %>% filter(treatment == "none") %>% rename(control = value) %>% select(subjectID, protein, control)) %>% 
     mutate(diff = value - control) %>% 
     select(subjectID, protein, treatment, diff) %>% 
     pivot_wider(names_from = treatment, values_from = diff, names_prefix = "diff_") %>% 
     group_by(protein) %>% 
       summarise(across(starts_with("diff"), mean, rm.na=TRUE))
  

Returns:

    protein  diff_drug1 diff_drug2
    <chr>         <dbl>      <dbl>
  1 proteinA       1.15      -1.4 
  2 proteinB      -1.20       1.55
library(tidyverse)

df <- data.frame(
  "subjectID" = c("S1", "S2", "S2", "S1", "S1", "S2", "S2", "S1", "S1", "S2", "S1", "S2"),
  "treatment" = c("none", "none", "none", "none", "drug1", "drug1", "drug1", "drug1", "drug2", "drug2", "drug2", "drug2"),
  "protein" = c("proteinA", "proteinA", "proteinB", "proteinB", "proteinA", "proteinA", "proteinB", "proteinB", "proteinA", "proteinA", "proteinB", "proteinB"),
  "value" = c(5.3, 4.3, 4.5, 2.3, 6.5, 5.4, 1.2, 3.2, 2.3, 4.5, 6.5, 3.4)
)

# For every pair of protein and drug treatment
expand_grid(
  protein = df$protein %>% unique(),
  comparison = df$treatment %>% unique() %>% setdiff("none")
) %>%
  mutate(
    mean_diff = comparison %>% map2_dbl(protein, ~ {
      df %>%
        pivot_wider(names_from = treatment, values_from = value) %>%
        filter(protein == .y) %>%
        rename_at(.x, ~"drug") %>%
        mutate(diff = none - drug) %>%
        pull(diff) %>%
        mean()
    })
  ) %>%
  pivot_wider(names_from = comparison, values_from = mean_diff, names_prefix = "mean_diff_")
#> # A tibble: 2 x 3
#>   protein  mean_diff_drug1 mean_diff_drug2
#>   <chr>              <dbl>           <dbl>
#> 1 proteinA           -1.15            1.4 
#> 2 proteinB            1.2            -1.55

reprex package (v2.0.1)

于 2021-10-05 创建