-
Notifications
You must be signed in to change notification settings - Fork 998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support function lag in data.table #965
Comments
Just as an FYI, On Thu, Nov 20, 2014 at 5:41 AM, Juris Breidaks notifications@github.com
|
Please read completely and follow the instructions here. Thank you. |
Implemented
x = 1:5
# [1] 1 2 3 4 5
c(NA, diff(x))
# [1] NA 1 1 1 1
c(NA, NA, diff(x, differences=2L)
# [1] NA NA 0 0 0
# If we want both of these in a list, then we
# should be able to accomplish this as:
shift(x, n=1:2, type="lagdiff")
|
Can include in object data.table lag function?
As example?
lag <- function(var, k) {
if (k > 0) {
#bring past valeus forward k times
return(c(rep(NA, k), head(var, -k)))
} else {
#bring future values backward
return(c(tail(var, k), rep(NA, -k)))
}
}
x = data.table(a=1:10, dte=sample(seq.Date(from=as.Date("2012-01-20"), to=as.Date("2012-01-30"), by=1),10))
x[, L1_a:=lag(a,1)]
The text was updated successfully, but these errors were encountered: