按组计算连续行的值之间的差异

时间:2022-09-06 15:22:44

This is a my df (data.frame):

这是我的df (data.frame):

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

I need to calculate difference between values in consecutive rows by group.

我需要按组计算连续行的值之间的差异。

So, I need a that result.

我需要一个那样的结果。

group value diff
1     10    NA # because there is a no previous value
1     20    10 # value[2] - value[1]
1     25    5  # value[3] value[2]
2     5     NA # because group is changed
2     10    5  # value[5] - value[4]
2     15    5  # value[6] - value[5]

Although, I can handle this problem by using ddply, but it takes too much time. This is because I have a lot of groups in my df. (over 1,000,000 groups in my df)

虽然我可以用ddply来解决这个问题,但是这需要很多时间。这是因为我的df中有很多组。(在我的df中超过100万组)

Are there any other effective approaches to handle this problem?

有其他有效的方法来处理这个问题吗?

3 个解决方案

#1


38  

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

包数据。通过使用shift函数,table可以相当快速地做到这一点。

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

或者在dplyr中使用滞后函数。

df %>%
    group_by(group) %>%
    mutate(Diff = value - lag(value))
#   group value  Diff
#   <int> <int> <int>
# 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.

选择pre-data。表:shift和pre-dplyr::lag, see edits。

#2


15  

You can use the base function ave() for this

你可以用函数ave()来做这个

df <- data.frame(group=rep(c(1,2),each=3),value=c(10,20,25,5,10,15))
df$diff <- ave(df$value, factor(df$group), FUN=function(x) c(NA,diff(x)))

which returns

它返回

  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

#3


4  

try this with tapply

试试这个,tapply

df$diff<-as.vector(unlist(tapply(df$value,df$group,FUN=function(x){ return (c(NA,diff(x)))})))

#1


38  

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

包数据。通过使用shift函数,table可以相当快速地做到这一点。

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

或者在dplyr中使用滞后函数。

df %>%
    group_by(group) %>%
    mutate(Diff = value - lag(value))
#   group value  Diff
#   <int> <int> <int>
# 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.

选择pre-data。表:shift和pre-dplyr::lag, see edits。

#2


15  

You can use the base function ave() for this

你可以用函数ave()来做这个

df <- data.frame(group=rep(c(1,2),each=3),value=c(10,20,25,5,10,15))
df$diff <- ave(df$value, factor(df$group), FUN=function(x) c(NA,diff(x)))

which returns

它返回

  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

#3


4  

try this with tapply

试试这个,tapply

df$diff<-as.vector(unlist(tapply(df$value,df$group,FUN=function(x){ return (c(NA,diff(x)))})))