是否有用于读取 date_isoweek 格式的日期的 R 函数?

Is there an R function for reading a Date in a date_isoweek format?

我有特定格式的数据 year_isoweek,例如本周的 2019_31。我希望将其表示为进一步处理的日期。

基本rlubridate
我发现了一些有用的解决方法,但是 none 这些方法与 ISO 8601 (%V) 一起工作,这又会导致其他问题:



a <- c("2019_7", "2019_3", "2018_18")
as.Date(a, "%Y_%V")

实际输出:

[1] "2019-07-31" "2019-07-31" "2018-07-31"

我想要的: "2019-02-11" "2019-01-14" "2018-04-30" 作为 class date

好的,我找到了使用包 ISOweek 的解决方案:

library(ISOweek)
library(stringr)
a <- c("2019", "2019", "2018")
b <- str_pad(c("7", "3", "18"), 2, pad = "0")
c <- ISOweek2date(paste0(a,"-W",b, "-",1))
str(c)

Date[1:3], format: "2019-02-11" "2019-01-14" "2018-04-30"
完全一样
"2019-02-11" "2019-01-14" "2018-04-30"如classdate如我所愿!

ISOweek 有一个不便之处,因为它不接受单个数字或单个字符串作为周数(例如,不接受“7”,只接受“07”)。因此,stringr包也是需要的。