Retain and lag function in R as SAS

后端 未结 4 606
礼貌的吻别
礼貌的吻别 2021-01-05 10:05

I am looking for a function in R similar to lag1, lag2 and retain functions in SAS which I can use with data.tables.

I know th

4条回答
  •  别那么骄傲
    2021-01-05 10:40

    here's an example: cumulate value with sqldf:

    > w_cum <-
     sqldf("select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as cum_sum
           from w_cum       t1
           inner join w_cum t2 on t1.id >= t2.id
           group by t1.id, t1.SomeNumt
           order by t1.id
    

    ")

       id SomeNumt cum_sum
    
    • 1 11 11
    • 2 12 23
    • 3 13 36
    • 4 14 50
    • 5 15 65
    • 6 16 81
    • 7 17 98
    • 8 18 116
    • 9 19 135
    • 10 20 155

提交回复
热议问题