Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

time series - Calculate first difference by group in R

I was wondering if someone could help me calculate the first difference of a score by group. I know it should be a simple process but for some reason I'm having trouble doing it..... yikes

Here's an example data frame:

score <- c(10,30,14,20,6)

group <- c(rep(1001,2),rep(1005,3))

df <- data.frame(score,group)

> df 
  score group
1    10  1001
2    30  1001
3    14  1005
4    20  1005
5     6  1005

And here's the output I was looking for.

1   NA
2   20
3   NA  
4    6
5  -14

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is one way using base R

df$diff <- unlist(by(df$score , list(df$group) , function(i) c(NA,diff(i))))

or

df$diff <- ave(df$score , df$group , FUN=function(i) c(NA,diff(i)))


or using data.table - this will be more efficient for larger data.frames

library(data.table)
dt <- data.table(df)
setkey(dt,group)
dt[,diff:=c(NA,diff(score)),by=group]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...