简体   繁体   中英

Grouped percent change in R data.table

I'd like to calculate the % diff in subsequent observations of an R data.table , grouping by a variable. For example, given a dataset like this:

dt <- data.table(group=c(rep("A", 3), rep("B", 3)),
                 index=rep(1:3, 2),
                 val=1:6)
#    group index val
# 1:     A     1   1
# 2:     A     2   2
# 3:     A     3   3
# 4:     B     1   4
# 5:     B     2   5
# 6:     B     3   6

I'd like a result like this, calculating % diff of val (first record goes from 1->2, so 100%, second 2->3 so 50% diff, etc.):

#    group index pct.diff
# 1:     A     1     1.00
# 2:     A     2     0.50
# 3:     B     1     0.25
# 4:     B     2     0.20

You can use diff and .N to remove the final record in each group:

dt[, list(index=index[-.N],
          pct.diff=diff(val) / val[-.N]),
   group]

This produces the result at the bottom of the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM