-
Notifications
You must be signed in to change notification settings - Fork 6
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
Implement dplyr::lead()
and dplyr::lag()
functions
#8
Comments
Thank you @christophscheuch for the kind words and for the suggestion! Can you try calling them with a preceding tilde, like this: If that works let us know (and I can explain why). If not, I'll take a look. Agree that we should have this functionality natively supported with Tidier.jl, whether or not we can borrow the implementation from ShiftedArrays.jl. I am also posting the dplyr implementation here as a placeholder in case we want to just base our implementation off of that: https://github.com/tidyverse/dplyr/blob/main/R/lead-lag.R Key things for our implementation:
|
Thanks for the quick response! So I tried the following to simply construct a lagged variable:
I get the following error messages:
Unfortunately, I do not know anything about these array types in Julia, so I'm lost :/ |
Have no fear. We will look into this and sort it out! |
Ok, I figured out the issue. While the And if you include This pattern is a bit more common in Julia because it's intended to protect your namespace. You would still be able to access the non-exported functions by writing So tl;dr: here's a solution that will work for now. Add the following code to the top of your script:
Then, if you use In an update in the near future, we'll directly add support for these functions within Tidier.jl and will remove the need for the tilde. The reason the tilde is needed for now is that Tidier.jl is "auto-vectorizing" Adding the tilde to the front prevents this auto-vectorization. In the next update, we will add these functions to our "do-not-vectorize" list, which will remove the need for a tilde. Additionally, I plan to expose a function that will allow users to manually add functions to that list. |
Indeed this works and I learned something - thanks a lot for the explanation! So for completeness, this code snippet now calcualtes returns :)
|
Note to self: I need to update the auto-vectorization docs to give an example when functions should not be auto-vectorized inside of While we will generally try to automate this, users need to understand this when using custom functions that they have written inside of Tidier verbs. |
This is now fixed in TidierOrg/Tidier.jl#82. Both functions are now included in the do-not-vectorize list, so there's no more need to use a tilde. I'll close this issue out after updating the docs for |
Your fantastic package motivated me to revisit Julia after more than 5 years of abstinence - please keep going :)
Would be great if
Tidier.jl
could support the computation of lagged or leading values in data frames. For instance, for the typical financial application, I'd want to compute returns over different horizons like this:I guess there is some workaround using
ShiftedArrays.jl
but I can't seem to make it work with my limited Julia experience 😞The text was updated successfully, but these errors were encountered: