Skip to content

Support function lag in data.table #965

Closed

Description

@JBreidaks

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

Activity

  1. gsee commented on Nov 20, 2014

    @gsee

    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.

  2. arunsrinivasan commented on Dec 1, 2014

    @arunsrinivasan
    Member

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

  3. added this to the v1.9.6 milestone on Jan 6, 2015
  4. arunsrinivasan commented on Jan 7, 2015

    @arunsrinivasan
    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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions