在data.table中按组对行进行滚动操作 [英] Rolling operation over rows by group in data.table

查看:58
本文介绍了在data.table中按组对行进行滚动操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按组对 data.table 的行进行滚动操作。

I would like to do roll an operation over the rows of a data.table by group.

在下面的示例中,我想获取2017年每个 id 并从2014年的值中减去。该解决方案不应涉及重塑数据。它必须创建一个新列,以显示两个ID在两年之间的值。

In this example below, I want to get the values of each id in 2017 and subtract from the value in 2014. The solution should not involve reshaping the data. It has to create a new column that will show how the value of each id changed between the two years.

library(data.table)

dt <- data.table( id=rep(letters[1:4],2),
                  values=c(11,22,33,44, 1,2,3,4),
                  year=c(rep(2014,4), rep(2017,4)))

#>  dt
#>     id values year
#>  1:  a     11 2014
#>  2:  b     22 2014
#>  3:  c     33 2014
#>  4:  d     44 2014
#>  5:  a      1 2017
#>  6:  b      2 2017
#>  7:  c      3 2017
#>  8:  d      4 2017

预期产量:

#> dt
#>    id values year result
#> 1:  a     11 2014     10
#> 2:  b     22 2014     20
#> 3:  c     33 2014     30
#> 4:  d     44 2014     40
#> 5:  a      1 2017     10
#> 6:  b      2 2017     20
#> 7:  c      3 2017     30
#> 8:  d      4 2017     40


推荐答案

使用起来非常简单

dt[, result := values[year == 2014] - values[year == 2017], by = id]
#   id values year result
#1:  a     11 2014     10
#2:  b     22 2014     20
#3:  c     33 2014     30
#4:  d     44 2014     40
#5:  a      1 2017     10
#6:  b      2 2017     20
#7:  c      3 2017     30
#8:  d      4 2017     40

另一个选项(不太明确)是 diff

Another option (less explicit) is with diff:

dt[order(-year), result := diff(values), by = id]

这篇关于在data.table中按组对行进行滚动操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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