You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just as an FYI, stats::lag is a generic. And, for what it's worth, xts
has a lag.xts() method written in C. There is also a generic quantmod::Lag
which has 6 methods. Not that it's all that relevant, other than something
to look at for ideas.
On Thu, Nov 20, 2014 at 5:41 AM, Juris Breidaks [email protected]
wrote:
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)]
—
Reply to this email directly or view it on GitHub #965.
Implemented shift(), which takes type = "lag" and type = "lead". The reasons are:
Avoid name clashes as much as possible.
These operations are related, and therefore makes sense to be arguments under a common name.
We can add more types. For example, I think adding type = "lagdiff" might be useful which is equivalent to doing c(NA, diff(.)), as I see this coming up a lot on SO. For example:
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")
Activity
gsee commentedon Nov 20, 2014
Just as an FYI,
stats::lagis a generic. And, for what it's worth, xtshas a lag.xts() method written in C. There is also a generic quantmod::Lag
which has 6 methods. Not that it's all that relevant, other than something
to look at for ideas.
On Thu, Nov 20, 2014 at 5:41 AM, Juris Breidaks [email protected]
wrote:
arunsrinivasan commentedon Dec 1, 2014
Please read completely and follow the instructions here. Thank you.
arunsrinivasan commentedon Jan 7, 2015
Implemented
shift(), which takestype = "lag"andtype = "lead". The reasons are:types. For example, I think addingtype = "lagdiff"might be useful which is equivalent to doingc(NA, diff(.)), as I see this coming up a lot on SO. For example:typeis cyclic shift.