问题
Sample data:
Date <- as.Date(c('1-01-2008','2-01-2008', '3-01-2008','4-01-2008', '5-01-2008', '1-01-2008','2-01-2008', '3-01-2008','4-01-2008', '5-01-2008'), format = "%m-%d-%Y")
Country <- c('US', 'US','US','US', 'US', 'JP', 'JP', 'JP', 'JP', 'JP')
Category <- c('Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Apple')
Value <- c(runif(10, -0.5, 10))
df <- data.frame(Date, Country, Category, Value)
I am using the following piece to calculate the lagged growth rate of Value within Country and within Category:
df <- ddply(df, .(Country, Category), transform,
Growth6m=c(NA, NA, NA, exp(diff(log(Value), lag = 3))-1))
Now I am trying to get lagged difference rather than growth rate. This worked fine for the first lag (i.e. subtracting value from the previous row) like this:
df <- ddply(df, .(Country, Category), transform,
Growth1m=c(NA, diff(Value)))
but when I introduce higher order lags (ex. subtracting the first row from the third row), I get the error such as: "arguments imply differing number of rows: 157, 158". I tried playing around with the NA's but to no avail.
Edit: sample data
回答1:
That's easy with dplyr
library(dplyr)
df %>%
group_by(Country, Category) %>%
mutate(
deltaLag1 = Value - lag(Value, 1),
deltaLag2 = Value - lag(Value, 2)
)
来源:https://stackoverflow.com/questions/35776233/lagged-differences