基于带有tidyverse的分类列计算差异 [英] Calculate differences based on categorical column with tidyverse

查看:53
本文介绍了基于带有tidyverse的分类列计算差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

library(tidyverse)

df <- data.frame(
  vars = rep(letters[1:2], 3),
  value = c(10,12,15,19,22,23),
  phase = rep(factor(c("pre","post1","post2"), levels = c("pre","post1","post2")),2)
) %>% 
  arrange(vars,phase)

我想计算以下内容的 value 中的差异:

And I would like to calculate the difference in value of the following:

  • post1-pre
  • post2-post1
  • post2-pre

每个 var (即 a b ).

使用 tidyverse 实现这一目标的最有效方法是什么?

What would be the most efficient way of achieving this using tidyverse?

预期结果:

 vars         x     diffs
    a   post1 - pre    12
    a post2 - post1    -7
    a   post2 - pre     5
    b   post1 - pre    -7
    b post2 - post1    11
    b   post2 - pre     4

推荐答案

您可以使用 tidyr 中的 spread gather ,先进行转换相移成列,然后计算出差值后再次放入长格式:

You can use spread and gather from tidyr, first to transform phase into columns, and then once difference is calculated to put into long format again:

library(dplyr)
library(tidyr)
df %>%
    spread(phase, value) %>%
    mutate("post1 - pre" = post1 - pre, "post2 - post1" = post2 - post1, "post2 - pre" = post2 - pre) %>%
    select(-pre, -post1, -post2) %>%
    gather("x", "diff", 2:4)

这篇关于基于带有tidyverse的分类列计算差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆