最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Week start on Mondays - Stack Overflow

programmeradmin2浏览0评论

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 Looks like a typo, you have 5 opening parens vs 3 closing. And I'm afraid there's no change_on_boundary arg for floor_date, only for ceiling_date – margusl Commented Mar 6 at 18:52
  • I'm not sure my answer below is responsive to your concern. Can you spell out more about what specific behavior you expect or want to test with change_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
  • I think what I needed was the floor function. However, it appeared to be not necessarily behaving as intended. However it turns out that there were errors quite deep into the dataset, thanks for all your help and sorry to be a nuisance. – Slayer25 Commented Mar 6 at 20:47
Add a comment  | 

1 Answer 1

Reset to default 0
data.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
发布评论

评论列表(0)

  1. 暂无评论