1

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!

Neal Barsch
  • 2,810
  • 2
  • 13
  • 39

3 Answers3

2
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"
Aurèle
  • 12,545
  • 1
  • 31
  • 49
1
sample_data <- c("Sun 11.30am", "Tues 01.00pm", "Mon 10.00am", "Sun 01.30am", "Thurs, 02.30pm")

today <- Sys.Date()
today_weekday <- as.numeric(format(today, "%u"))
day <- tolower(substr(sample_data, 1, 3))
day_diff <- match(day, c("mon", "tue", "wed", "thu", "fri", "sat", "sun"))
dayR <- today + ((day_diff + 7) - today_weekday) %% 7
dayR
#[1] "2023-03-19" "2023-03-21" "2023-03-20" "2023-03-19" "2023-03-23"

time <- substr(sample_data, nchar(sample_data) - 6, nchar(sample_data))
time
#[1] "11.30am" "01.00pm" "10.00am" "01.30am" "02.30pm"

final <- as.POSIXct(paste(dayR, time), format = "%Y-%m-%d %H.%M%p")
final_formatted <- format(final, "%Y-%m-%d %H:%M%p")
final_formatted
#[1] "2023-03-19 11:30AM" "2023-03-21 01:00AM" "2023-03-20 10:00AM" "2023-03-19 01:30AM" "2023-03-23 02:30AM"
Maël
  • 45,206
  • 3
  • 29
  • 67
-1

A base R approach, first getting the day that matches, then combining the date and the formatted time

DYS <- seq.Date(Sys.Date(), Sys.Date() + 7, "day")

DYS_n <- sapply(sub(" .*", "", sample_data), agrep, weekdays(DYS))

data <- as.POSIXct(paste(DYS[DYS_n], sub(".* ", "", sample_data)), 
  format = "%Y-%m-%d %I.%M%p")

format(data, "%F %I:%M%p")
[1] "2023-03-19 11:30AM" "2023-03-21 01:00PM" "2023-03-20 10:00AM"
[4] "2023-03-19 01:30AM" "2023-03-23 02:30PM"
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29