I have an irregular time series, where there are gaps in the dataset. Further, the data is grouped. The lag functions I have been able to find lag by observation (so they find the prior record in the dataset), but I want to specify a time variable and have the lag calculated by matching the lagged time. This question: R lag/lead irregular time series data is doing a similar thing. However, I can't use zoo
solution (I have some sort of package incompatibility and can't use zoo
at all) and have been unsuccessful in making the data.table
solution into something sufficiently flexible to use as a function with lag amount as an input and the capacity for grouped data.
Test data:
testdf <- data.frame(group = c(1,1,1,1,1,2,2,2,2,2),
counter = c(1,2,3,5,6,7,8,9,11,12),
xval = seq(100, 1000, 100))
lagamount <- 1
The output should be the vector: NA 100 200 NA 400 NA 600 700 NA 900
This is what I am using at the moment:
library(dplyr)
testout <- group_by(testdf, group) %>%
mutate(testout = function(x) which((testdf$counter - x) == lagamount))
This gives me a datatype error that something (unspecified) is not a vector.
Is there a way to make this construction work? Alternatively, how could I lag with irregular time series with grouped variables?