I am having problems using floor date rounding to the nearest Monday with the week commencing on a Monday. I'd like to use change_on_boundary to establish whether the default is causing problems in the real data set but for some reason mutate does not seem to work.
The example below is not the actual problem due to confidentiality reasons but can be reproduced. I have looked at the documentation and cannot find a similar example in terms of synthax.
library(lubridate)
library(dplyr)
Date <- c("2025-03-02","2025-03-02","2025-03-02")
Area <- c("A","B","C")
UnitAlpha <- data.frame(Date,Area)
UnitAlpha <- UnitAlpha %>%
mutate(WC.Date = floor_date((ymd(Date),unit="week",change_on_boundary=FALSE,
week_start = getOption("lubridate.week.start", 1))
This leads to the following error: -
Error: unexpected ',' in:
"UnitAlpha <- UnitAlpha %>%
mutate(WC.Date = floor_date((ymd(Date),"
How do I fix this?
I am having problems using floor date rounding to the nearest Monday with the week commencing on a Monday. I'd like to use change_on_boundary to establish whether the default is causing problems in the real data set but for some reason mutate does not seem to work.
The example below is not the actual problem due to confidentiality reasons but can be reproduced. I have looked at the documentation and cannot find a similar example in terms of synthax.
library(lubridate)
library(dplyr)
Date <- c("2025-03-02","2025-03-02","2025-03-02")
Area <- c("A","B","C")
UnitAlpha <- data.frame(Date,Area)
UnitAlpha <- UnitAlpha %>%
mutate(WC.Date = floor_date((ymd(Date),unit="week",change_on_boundary=FALSE,
week_start = getOption("lubridate.week.start", 1))
This leads to the following error: -
Error: unexpected ',' in:
"UnitAlpha <- UnitAlpha %>%
mutate(WC.Date = floor_date((ymd(Date),"
How do I fix this?
Share Improve this question edited Mar 6 at 18:30 Jan 10.2k6 gold badges21 silver badges33 bronze badges asked Mar 6 at 18:19 Slayer25Slayer25 133 bronze badges 3 |1 Answer
Reset to default 0data.frame(Date = as.Date("2025-03-06") + 0:8) |>
dplyr::mutate(WC.Date = lubridate::round_date(Date, "week", week_start = 1),
dow = lubridate::wday(Date, label = TRUE))
Would "round_date" work for you? Here I have Fri-Sun rounding up to the next Monday, and Tues-Thu rounding back to the Monday that came before. I think this would be equivalent to using floor_date(Date+3...)
, or ceiling_date(Date-3...)
.
Date WC.Date dow
1 2025-03-06 2025-03-03 Thu
2 2025-03-07 2025-03-10 Fri
3 2025-03-08 2025-03-10 Sat
4 2025-03-09 2025-03-10 Sun
5 2025-03-10 2025-03-10 Mon
6 2025-03-11 2025-03-10 Tue
7 2025-03-12 2025-03-10 Wed
8 2025-03-13 2025-03-10 Thu
9 2025-03-14 2025-03-17 Fri
change_on_boundary
arg forfloor_date
, only forceiling_date
– margusl Commented Mar 6 at 18:52change_on_boundary=FALSE
? What is the difference between "rounding to the nearest Monday with the week commencing on a Monday" and "rounding to the nearest Monday"? Do you mean "floor to the most recent Monday"? – Jon Spring Commented Mar 6 at 19:13