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

r - Find a date based on the knowledge of a weekday and a date earlier in the week - Stack Overflow

programmeradmin3浏览0评论

My goal is to find the date on a day later in the week based on the information I already have which is another date earlier in the week. In this instance I have a date of 2025-02-16 which I can use weekdays() to work out is a Monday. I now want to find dates for all other days in the week Monday to Sunday, ideally returning the days and dates in a data frame for onward use (this part isn't essential).

theDate = "2025-02-17"
theDay = "Monday"

My initial though was to do this in a while loop like so, although this works I feel like there are better ways to do what I need as I would have to repeat this a few times.

while(weekdays(as.Date(theDate))!="Sunday"){
  myDate = theDate+1
}

My end goal is something along the lines of;

datesdf
Monday     Tuesday    Wednesday  Thursday   Friday     Saturday   Sunday
2025-02-16 2025-02-17 2025-02-18 2025-02-19 2025-02-20 2025-02-21 2025-02-22

My goal is to find the date on a day later in the week based on the information I already have which is another date earlier in the week. In this instance I have a date of 2025-02-16 which I can use weekdays() to work out is a Monday. I now want to find dates for all other days in the week Monday to Sunday, ideally returning the days and dates in a data frame for onward use (this part isn't essential).

theDate = "2025-02-17"
theDay = "Monday"

My initial though was to do this in a while loop like so, although this works I feel like there are better ways to do what I need as I would have to repeat this a few times.

while(weekdays(as.Date(theDate))!="Sunday"){
  myDate = theDate+1
}

My end goal is something along the lines of;

datesdf
Monday     Tuesday    Wednesday  Thursday   Friday     Saturday   Sunday
2025-02-16 2025-02-17 2025-02-18 2025-02-19 2025-02-20 2025-02-21 2025-02-22
Share Improve this question edited 1 hour ago jpsmith 17.3k6 gold badges20 silver badges45 bronze badges asked 1 hour ago JoeJoe 1,2634 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

lubridate's floor and ceiling functions for dates will help with this:

library(lubridate)
full_week = function(day) {
  seq.Date(
    from = floor_date(day, unit = "week"),
    to = ceiling_date(day, unit = "week", change_on_boundary = FALSE),
    by = "day"
  )
}

theDate = "2025-02-17"
full_week(ymd(theDate))
# [1] "2025-02-16" "2025-02-17" "2025-02-18" "2025-02-19" "2025-02-20" "2025-02-21" "2025-02-22"

That should be enough to get you started - if you want to put the resulting vector dates in a data frame and name the columns with the day names, I'll leave that to you.

1) cut.Date Convert theDate to Date class and use cut to get prior Monday or the current date if it is already Monday. Adding 0:6 gives all dates in the week. Set the names using weekdays and then keep only dates greater or equal to theDate. No packages are used.

theDate <- "2025-02-17"

dates <- as.Date(cut(as.Date(theDate), "week")) + 0:6
dates <- setNames(dates, weekdays(dates))
next_dates <- dates[dates >= theDate]
next_dates

##       Monday      Tuesday    Wednesday     Thursday       Friday     Saturday       Sunday 
## "2025-02-17" "2025-02-18" "2025-02-19" "2025-02-20" "2025-02-21" "2025-02-22" "2025-02-23" 

2) nextsun We can create a function nextsun to calculate the date of the next Sunday or today if it is Sunday and then use seq. This also uses only base R. Output is as above.

nextsun <- function(x) 7 * ceiling(as.numeric(x - 0 + 4)/7) + as.Date(0-4)
next_dates <- seq(as.Date(theDate), nextsun(as.Date(theDate)), "day")
setNames(next_dates, weekdays(next_dates))

You could add a sequence to the date and subset the weekdays by 1str complete bin.

> week_fun <- \(theDate, start_week='Monday') {
+   data.frame(
+     date=as.Date(theDate) + seq_len(13) - 7
+   ) |>
+     transform(weekday=weekdays(date)) |> 
+     subset(cumsum(weekday == start_week) == 1)
+   
+ }
> 
> week_fun("2025-02-17")
         date   weekday
7  2025-02-17    Monday
8  2025-02-18   Tuesday
9  2025-02-19 Wednesday
10 2025-02-20  Thursday
11 2025-02-21    Friday
12 2025-02-22  Saturday
13 2025-02-23    Sunday
> week_fun("2025-02-19")
         date   weekday
5  2025-02-17    Monday
6  2025-02-18   Tuesday
7  2025-02-19 Wednesday
8  2025-02-20  Thursday
9  2025-02-21    Friday
10 2025-02-22  Saturday
11 2025-02-23    Sunday
> week_fun("2025-02-19", 'Sunday')
         date   weekday
4  2025-02-16    Sunday
5  2025-02-17    Monday
6  2025-02-18   Tuesday
7  2025-02-19 Wednesday
8  2025-02-20  Thursday
9  2025-02-21    Friday
10 2025-02-22  Saturday
发布评论

评论列表(0)

  1. 暂无评论