Calculate difference between values in consecutive rows by group

后端 未结 3 689
遥遥无期
遥遥无期 2020-11-22 10:14

This is a my df (data.frame):

group value
1     10
1     20
1     25
2     5
2     10
2     15 

I need to calculate difference

3条回答
  •  盖世英雄少女心
    2020-11-22 10:36

    The package data.table can do this fairly quickly, using the shift function.

    require(data.table)
    df <- data.table(group = rep(c(1, 2), each = 3), value = c(10,20,25,5,10,15))
    #setDT(df) #if df is already a data frame
    
    df[ , diff := value - shift(value), by = group]    
    #   group value diff
    #1:     1    10   NA
    #2:     1    20   10
    #3:     1    25    5
    #4:     2     5   NA
    #5:     2    10    5
    #6:     2    15    5
    setDF(df) #if you want to convert back to old data.frame syntax
    

    Or using the lag function in dplyr

    df %>%
        group_by(group) %>%
        mutate(Diff = value - lag(value))
    #   group value  Diff
    #     
    # 1     1    10    NA
    # 2     1    20    10
    # 3     1    25     5
    # 4     2     5    NA
    # 5     2    10     5
    # 6     2    15     5
    

    For alternatives pre-data.table::shift and pre-dplyr::lag, see edits.

提交回复
热议问题