gdemin / maditr

Fast Data Aggregation, Modification, and Filtering
61 stars 3 forks source link

Get lag and lead using API #13

Closed hope-data-science closed 4 years ago

hope-data-science commented 4 years ago

It is great design to have "by" parameter in dt_mutate and dt_summarise, however, when I try:

iris %>% dt_mutate(id = lag(Sepal.Length,2)) %>% as.data.table

It did not work. Is there some way to make it work? Should we design a new API or just optimize it inside dt_mutate?

gdemin commented 4 years ago

@hope-data-science It seems you use lag from base R which has very surprising behaviour:

lag(1:3)
# [1] 1 2 3
# attr(,"tsp")
# [1] 0 2 1

There is a special function shift in the data.table package. It should work as you expect from lag.