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

Use %/% within a pipe

I would like to get interval in days as an integer between two dates:

library(tidyverse) # esp for lubridate

date1 <- ymd('2022-01-01')
date2 <- ymd('2022-01-10')
day_interval <- interval(date1, date2)
day_interval %/% days() # returns 9

But I want to combine the 2 last lines into one. Is this possible? Tried e.g:

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

day_interval <- interval(date1, date2) |> %/% days()
Error: unexpected SPECIAL in "day_interval <- interval(date1, date2) |> %/%"

How can I do the whole think in a oner using a pipe?

>Solution :

We could use

interval(date1, date2) %/% days() 
[1] 9

Or may also use

interval(date1, date2) %>% 
  `%/%`(., days())
[1] 9

Or with |>

interval(date1, date2) |> 
   {\(x) x %/% days()}()
[1] 9

Or using %>%

interval(date1, date2) %>%
   {. %/% days()}
[1] 9
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