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

Lubridate not working when using floor_date

Hi I have a piece of code that rounds a range of dates in dataframe the strange is that it works for a one dataframe but for the another dataframe it gives an error

so for one dataframe I have this code

> range(data1$seamless_user_reg_date)
[1] "2021-05-03" "2022-07-11"

> lubridate::floor_date(range(data1$seamless_user_reg_date), "month")
[1] "2021-05-01" "2022-07-01"

and for the other dataframe where i get the error I have this code

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

> range(data1$opt_meeting_booked_date)
[1] "2021-06-24" "2022-05-03"

> lubridate::floor_date(range(data1$opt_meeting_booked_date), "month")
Error in object[[name, exact = TRUE]] : subscript out of bounds

what I would need on the second result is

"2021-06-01" "2022-05-01"

Any help on why this could be happening?

>Solution :

The second column, opt_meeting_booked_date, is not a date or datetime column. You have to cast the values from strings to dates.

library("lubridate")

x <- c("2021-06-24", "2022-05-03")

lubridate::floor_date(x, "month")
#> Error in object[[name, exact = TRUE]]: subscript out of bounds
lubridate::floor_date(as_date(x), "month")
#> [1] "2021-06-01" "2022-05-01"

Created on 2022-07-26 by the reprex package (v2.0.1)

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