I have data like this:
sample_data <- c("Sun 11.30am", "Tues 01.00pm", "Mon 10.00am", "Sun 01.30am", "Thurs, 02.30pm")
I want to return the year/month/day/time based on the weekdays, assuming the weekday is the next occurrence of the day (desired data is based on the date I posted this question, friday March 17):
desired_data <- c("2023-03-19 11:30AM", "2023-03-21 01:00PM", "2023-03-20 10:00AM","2023-03-19 01:30AM","2023-03-23 02:30PM")
Is there an easy R function to do this? Thanks!
library(lubridate)
start_date <- as.Date("2023-03-17")
lapply(sample_data, function(x) {
res <- start_date
wday(res, week_start = wday(start_date) - 1) <- stringr::str_extract(x, "^[:alpha:]+")
time_of_the_day <- parse_date_time(x, "%H %M %p")
hour(res) <- hour(time_of_the_day)
minute(res) <- minute(time_of_the_day)
res
})
[[1]]
[1] "2023-03-19 11:30:00 UTC"
[[2]]
[1] "2023-03-21 13:00:00 UTC"
[[3]]
[1] "2023-03-20 10:00:00 UTC"
[[4]]
[1] "2023-03-19 01:30:00 UTC"
[[5]]
[1] "2023-03-23 14:30:00 UTC"