R语言 如何根据条件计算同一列值之间的差异

m4pnthwp  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(231)

我有一个包含成对值的长df。我需要计算时间点之间的差异。

ID    Sample_ID  Treatment Timepoint  Score
   Amu      A          Contr      0       10
   Amu      A          Contr      1       5
   Amu      A          Treat      0       11
   Amu      A          Treat      1       7
   Amu      B          Contr      0       9
   Amu      B          Contr      1       4
   Amu      B          Treat      0       10
   Amu      B          Treat      1       6

我想做的是按ID、sample_ID、治疗分组,然后计算时间点“1”和“0”之间的评分值差异。我应该得到:

Amu_A_Contr = (5-10)
 Amu_A_Treat = (7-11)
 etc.

我尝试使用以下代码:

group_by(ID,Sample_ID, Treatment)%>%
  mutate(Diff = lead(Score) - Score )

但是分组功能,由于某种原因失败了,因此如果有人能帮忙就太好了。
谢谢你:)

byqmnocz

byqmnocz1#

我的方法是将时间点转换为列,然后直接进行比较

library(dplyr)
library(tidyr)

df <- data.frame(ID = rep('AMU', 8),
                 Sample_ID = c(rep('A', 4), rep('B', 4)),
                 Treatment = c('Contr', 'Contr', 'Treat', 'Treat', 'Contr', 'Contr', 'Treat', 'Treat'),
                 Timepoint = rep(c(0, 1), 4),
                 Score = c(10, 5, 11, 7, 9, 4, 10, 6)
                 )

df %>% 
  pivot_wider(id_cols = c(ID, Sample_ID, Treatment), 
              names_from = 'Timepoint', 
              values_from = 'Score', 
              names_glue = "time_{Timepoint}") %>% 
  mutate(diff = time_1 - time_0)

相关问题