Skip to content
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

Closed
JBreidaks opened this issue Nov 20, 2014 · 3 comments
Closed

Support function lag in data.table #965

JBreidaks opened this issue Nov 20, 2014 · 3 comments
Assignees
Milestone

Comments

@JBreidaks
Copy link

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)]

@gsee
Copy link

gsee commented Nov 20, 2014

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 notifications@github.com
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.

@arunsrinivasan
Copy link
Member

Please read completely and follow the instructions here. Thank you.

@arunsrinivasan arunsrinivasan self-assigned this Jan 6, 2015
@arunsrinivasan arunsrinivasan added this to the v1.9.6 milestone Jan 6, 2015
@arunsrinivasan
Copy link
Member

Implemented shift(), which takes type = "lag" and type = "lead". The reasons are:

  1. Avoid name clashes as much as possible.
  2. These operations are related, and therefore makes sense to be arguments under a common name.
  3. 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")
  1. Another type is cyclic shift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants