Lagging Variables in R

前端 未结 4 1231
闹比i
闹比i 2020-12-23 23:45

What is the most efficient way to make a matrix of lagged variables in R for an arbitrary variable (i.e. not a regular time series)

For example:

4条回答
  •  鱼传尺愫
    2020-12-24 00:11

    The running function in the gtools package does more or less what you want:

    > require("gtools")
    > running(1:4, fun=I, width=3, allow.fewer=TRUE)
    
    $`1:1`
    [1] 1
    
    $`1:2` 
    [1] 1 2
    
    $`1:3` 
    [1] 1 2 3
    
    $`2:4` 
    [1] 2 3 4
    

提交回复
热议问题