如何使用其他列中的值作为键减去特定列的值

时间:2018-08-16 23:34:28

标签: r tidyverse

如何使用c作为参考(事前)并以bd作为键,减去最后三列的值?

输入

df1 <- tribble(
  ~a, ~b, ~c, ~d, ~e, ~f, ~g,
  "1396-006_post_B", "1396-006", "post", "B", 18, 16, 0,
  "1396-006_post_L", "1396-006", "post", "L", 7, 15, 0,
  "1396-006_pre_B", "1396-006", "pre", "B", 16, 9, 0,
  "1396-006_pre_L", "1396-006", "pre", "L", 2, 1, 0,
  "1396-053_post_B", "1396-053", "post", "B", 25, 58, 0,
  "1396-053_pre_B", "1396-053", "pre", "B", 24, 27, 0
)

输出

1396-006_B  2 7 0
1396-006_L  5 14  0
1396-053_B  1 31  0

尝试

df1 %>% 
  group_by(b, d) %>% 
  mutate_all(funs(diff))%>%
  ungroup()

2 个答案:

答案 0 :(得分:2)

也许有更好的方法,但是我同意了

(df1 
    %>% group_by(b,d) 
    %>% arrange(c)
    %>% summarise_at(vars(e:g),funs(-1*diff(.)))
)
  • 使用summarise_at()而非mutate_at()
  • 使用-1*diff(.)获取正确的符号,并使用arrange(c)确保顺序正确。或者,您可以使用(1)arrange(desc(c))funs(diff)或(2)明确引用前置/后置的内容,例如funs(.[c=="post"]-.[c=="pre"])(我认为后者会更明确)
  • 也许最原则的方法是spread()分离前/后列,然后计算差异...但是我不确定如何获取多个匹配对的差异列...

答案 1 :(得分:0)

如果我正确理解了数据,可以使用reshape2库并更改键来完成:

library(reshape2)

df1 <- mutate(df1, key = paste(b,d, sep = "-"))
df2 <- dcast(melt(df1), key ~ c + variable)
df2 %>% mutate(diff_e = post_e - pre_e,
                diff_f = post_f - pre_f,
                diff_g = post_g - pre_g)

给出事例观察的前,后和差异:

           key   post_e post_f post_g pre_e pre_f pre_g diff_e diff_f diff_g
  1 1396-006-B     18     16      0    16     9     0      2      7      0
  2 1396-006-L      7     15      0     2     1     0      5     14      0
  3 1396-053-B     25     58      0    24    27     0      1     31      0