Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In a single chain get a max date – n years

(Better title wording recommendations welcome)

pacman::p_load(tidyverse, fable, fpp3, feasts)
aus_livestock$Month |> max() |> as_date()
[1] "2018-12-01"

I want to get this date minus 6 years in one line. Tried:

aus_livestock$Month |> max() |> as_date() |> -years(6)
Error: function '-' not supported in RHS call of a pipe

Then tried curly braces {}:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

aus_livestock$Month |> max() |> as_date() |> {. -years(6)}
Error: function '{' not supported in RHS call of a pipe

This does work:

x <- aus_livestock$Month |> max() |> as_date()
x - years(6)
[1] "2012-12-01"

But this involves saving an intermediary variable x. I’d prefer to do it in a oner if there’s a way?

>Solution :

We could use a lambda function

aus_livestock$Month |>
    max() |> 
    as_date() |> 
    (\(x) x - years(6))()
[1] "2012-12-01"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading