Home > Blockchain >  Lubridate not working when using floor_date
Lubridate not working when using floor_date

Time:07-26

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

> 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?

CodePudding user response:

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)

  • Related