作者:Adonis-唯一 | 来源:互联网 | 2023-05-26 15:12
1> talat..:
这是一种方法:
library(dplyr)
df %>%
group_by(User) %>%
mutate(x = Current_Coins - lag(Current_Coins)) %>% # compute the differences
summarise(Coin_gained = sum(x[x>0], na.rm = TRUE), # sum up positives
Coin_used = abs(sum(x[x<0], na.rm = TRUE))) # sum up negatives
#Source: local data frame [3 x 3]
#
# User Coin_gained Coin_used
#1 1 0 50
#2 2 50 0
#3 3 150 250