滞后于数据表 [英] lagging in data.table R

查看:100
本文介绍了滞后于数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个效用函数 lags data.table 中按组。函数很简单:

  panel_lag<  -  function(var,k){
if(k> 0 ){
#使过去的值向前k倍
return(c(rep(NA,k),head(var,-k)))
} else {
#未来值向后
return(c(tail(var,k),rep(NA,-k)))
}
}
pre>

然后我可以从 data.table 调用此方法:

  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:= panel_lag(a,1) #这不会正确工作`x`不是由日期键入
setkey(x,dte)
x [,L1_a:= panel_lag(a,1)]#这将

这需要我检查 panel_lag c> x 是键控的。有更好的方法做滞后吗?表格往往很大,所以他们应该真正键入。我只是在我迟到之前做 setkey 。我想确保我不忘记锁定他们。所以我想知道是否有一个标准的方式,人们做这个。

解决方案

如果你想确保你迟到可以使用 order 函数:

 虽然如果你''',你可以使用这个方法,但是你可以使用这个方法,按照日期顺序做很多事情,这样做是有意义的。


Currently I have a utility function that lags things in data.table by group. The function is simple:

panel_lag <- function(var, k) {
  if (k > 0) {
    # Bring past values forward k times
    return(c(rep(NA, k), head(var, -k)))
  } else {
    # Bring future values backward
    return(c(tail(var, k), rep(NA, -k)))
  }
}

I can then call this from a data.table:

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:=panel_lag(a, 1)]  # This won't work correctly as `x` isn't keyed by date
setkey(x, dte)
x[, L1_a:=panel_lag(a, 1)]  # This will

This requires that I check inside panel_lag whether x is keyed. Is there a better way to do lagging? The tables tend to be large so they should really be keyed. I just do setkey before i lag. I would like to make sure I don't forget to key them. So I would like to know if there is a standard way people do this.

解决方案

If you want to ensure that you lag in order of some other column, you could use the order function:

x[order(dte),L1_a:=panel_lag(a,1)]

Though if you're doing a lot of things in date order it would make sense to key it that way.

这篇关于滞后于数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆