R Data table - how to use previous row value within group [duplicate]

只愿长相守 提交于 2021-02-17 05:40:36

问题


I wish to calculate the difference between the current row and previous row, by groups.

x = data.table(a=c(15, 25, 10, 12), b = c(1,1,2,2))
> x
    a b
1: 15 1
2: 25 1
3: 10 2
4: 12 2
> x[, c:= a - c(NA, a[.I-1]), by=b]
Warning messages:
1: In a - c(NA, a[.I - 1]) :
  longer object length is not a multiple of shorter object length
2: In `[.data.table`(x, , `:=`(c, a - c(NA, a[.I - 1])), by = b) :
  RHS 1 is length 3 (greater than the size (2) of group 2). The last 1 element(s) will be discarded.

What I wish to obtain is:

> x
    a b  c
1: 15 1 NA
2: 25 1 10
3: 10 2 NA
4: 12 2  2

回答1:


#Using development version of data.table    

x[,value:=a-shift(a,1,type="lag"),by=b]
    a b value
1: 15 1    NA
2: 25 1    10
3: 10 2    NA
4: 12 2     2


来源:https://stackoverflow.com/questions/30405416/r-data-table-how-to-use-previous-row-value-within-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!