将工作日转换为R中的日期

jpfvwuh4  于 2023-03-20  发布在  其他
关注(0)|答案(3)|浏览(125)

我有这样的数据:

sample_data <- c("Sun 11.30am", "Tues 01.00pm", "Mon 10.00am", "Sun 01.30am", "Thurs, 02.30pm")

我希望返回基于工作日的年/月/日/时间,假设工作日是该日的下一个出现日期(所需数据基于我发布此问题的日期,3月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")

有没有简单的R函数可以做到这一点?谢谢!

3okqufwl

3okqufwl1#

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"
hyrbngr7

hyrbngr72#

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"
l5tcr1uw

l5tcr1uw3#

一种base R方法,首先获取匹配的日期,然后组合日期和格式化时间

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"

相关问题