Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shift type=cyclic #4451

Closed
RicoDiel opened this issue May 15, 2020 · 4 comments · Fixed by #5134
Closed

shift type=cyclic #4451

RicoDiel opened this issue May 15, 2020 · 4 comments · Fixed by #5134

Comments

@RicoDiel
Copy link

The Syntax of shift() is
shift(x, n=1L, fill=NA, type=c("lag", "lead", "shift"), give.names=FALSE)
For n=1, one can use fill = x[.N] to put the last value on top of the column, so one can "rotate" the values by 1.

When n>2, x[.N] will only repeat the last value in all the places to fill. An for me intuitive way to use shift would be
n = a, fill = x[(.N-a):.N]
which should then continue this "rotating" behavior. If it is attempted, the result is

Error : fill must be a vector of length 1

I would greatly appreciate if you could incorporate this functionality.

@jangorecki
Copy link
Member

jangorecki commented May 15, 2020

Thank you for filling FR. It would help if you could provide a reproducible and minimal example of input and expected output, not just the syntax.

@jangorecki
Copy link
Member

I seems the me that you are asking about type="cyclic" mentioned in #1708. Once you will provide input and output, it will be clear.

@RicoDiel
Copy link
Author

Thank you for your quick response. I will use an example close to the shift documentation:
DT = data.table(x=1:6, g=rep(1:2, each=3))
DT[ , shift(x, n = 1:2, fill=x[.N], type="lag"), by=g]
gives

g V1 V2
1 3 3
1 1 3
1 2 1
2 6 6
2 4 6
2 5 4

I proposed to allow fill = x[(.N-a):.N]
However, in the mentioned post from you it is proposed as cyclic shift. So the syntax in my mind would be
DT[ , shift(x, n = 1:2, fill=x[(.N-2):.N], type="lag"), by=g]
The syntax from the other post would be
DT[ , shift(x, n = 1:2, type="cyclic"), by=g]
but in either case, the expected output is

g V1 V2
1 3 2
1 1 3
1 2 1
2 6 5
2 4 6
2 5 4

Someone in #1708 also said they can't think of a real life example. I want to use it to shift prices from one market to another, to use them as instruments in estimating an "Almost Ideal Demand System" (AIDS).
I see an advantage in the "cyclic" that it is more simplistic, my proposal is more flexible. A downside from my proposal is, that fill is of length two here, and for V1 there is only one value, while the two values are present for V2. Ofcourse, you could allow inserting matrixes for filling, but i feel like that would start contradicting the elegant and very smart stlye of data.table in addition to being an extreme corner case, so i agree with the post and would wish a "cyclic" option.

@jangorecki jangorecki changed the title Shift's fill should allow a Vector bigger 1 shift type=cyclic May 17, 2020
@arunsrinivasan
Copy link
Member

I had this in mind when writing the function, but didn't see much of a demand on SO or here. So postponed it at that time. It should be fairly straightforward to add this feature though.

@ben-schwen ben-schwen mentioned this issue Sep 5, 2021
4 tasks
@mattdowle mattdowle added this to the 1.14.3 milestone Sep 24, 2021
@jangorecki jangorecki modified the milestones: 1.14.9, 1.15.0 Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants