is there a way to use lubridates:floor_date() in a postgres backend with non-standart units like ‘2 month’? or good alternatives?? I have a lazy tbl with a date column and want to group this column by x months.
Like so..
tbl(con_postgres, "testdata") %>%
mutate(floor_date(datecolumn, unit = "2 month"))
but this causes:
Caused by error in `floor_date()`:
! `unit` must be one of "second", "minute", "hour", "day", "week", "month", "quarter", or "year", not "2 month".
i Did you mean "month"?
any ideas?
the data as it is:
data = tibble(
Datum = seq.Date(
from = as_date("2024-01-01"),
to = as_date("2024-12-01"),
by = "month"))
data
# A tibble: 12 x 1
Datum
<date>
1 2024-01-01
2 2024-02-01
3 2024-03-01
4 2024-04-01
5 2024-05-01
6 2024-06-01
7 2024-07-01
8 2024-08-01
9 2024-09-01
10 2024-10-01
11 2024-11-01
12 2024-12-01
the data as i want it to be:
data %>%
mutate(
Datum = floor_date(Datum, unit = "2 month"))
# A tibble: 12 x 1
Datum
<date>
1 2024-01-01
2 2024-01-01
3 2024-03-01
4 2024-03-01
5 2024-05-01
6 2024-05-01
7 2024-07-01
8 2024-07-01
9 2024-09-01
10 2024-09-01
11 2024-11-01
12 2024-11-01
or with ‘6 month’
data %>%
mutate(
Datum = floor_date(Datum, unit = "6 month"))
# A tibble: 12 x 1
Datum
<date>
1 2024-01-01
2 2024-01-01
3 2024-01-01
4 2024-01-01
5 2024-01-01
6 2024-01-01
7 2024-07-01
8 2024-07-01
9 2024-07-01
10 2024-07-01
11 2024-07-01
12 2024-07-01
2
Answers
create a local dataset like:
and left_join it to postgres backend:
seems like a good practice and solved it for me
I’ll extend my answer up at the top leaving my original posting below.
for postgress you can look at dbplyr’s implementation of lubridate::floor_date for clues as to how it works :
So for postgress floor_date is implemented by a close as possible translation to a sql expression using postgres
DATE_TRUNC
functionality.So you can presumable submit sequal that uses the sorts of logic I described below. I.e. you could run this sort of SQL on a postgress db :
Now, how to make this play nice in dbplyr is probably another question that needs answering, I dont have time to explore that.
Now back in time to my original post…
I hinted at this in my comment, but there is no consensus or standard understanding of what it means to floor a date by two months.
I.e. perhaps its meant to group even, or odd months for example ?
should jan feb mar apr be transformed to jan jan mar mar or feb feb apr apr ?
Anyway, I made an example of these two possibilities.