将 year.week 转换为日期

Convert year.week to date

我的时间格式是 YYYY.week:

x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01", "2017.02", "2017.03", "2017.04", "2017.05")

有没有办法像这样创建将其转换为日期变量(日期不正确,但这是所需的格式):

as.Date(x[1])
"2017-02-01"

一个lubridate选项可以是:

ymd(paste0(substr(x, 1, 4), "-01", "-01"))+weeks(as.numeric(substr(x, nchar(x)-1, nchar(x)))-1)

 [1] "2016-12-02" "2016-12-09" "2016-12-16" "2016-12-23" "2017-01-01"

示例数据:

x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01")

如果您附加一个工作日,那么 as.Date 将有效:

x <- c("2016.49", "2016.50", "2016.51")

as.Date(paste0(x, ".1"), "%Y.%U.%u")

[1] "2016-12-05" "2016-12-12" "2016-12-19"

还有一个lubridate选项

parse_date_time(paste0(x,'.Mon'),'Y/W/a')
[1] "2016-12-05 UTC" "2016-12-12 UTC" "2016-12-19 UTC" "2016-12-26 UTC" "2017-01-02 UTC"