R:如何从年周中提取完整日期?
R: how to extract full date from year week?
我有一个格式如下的日期(年份和周数):
year_week
201804
201938
201745
201402
201510
我需要获取格式完整的日期:2018-02-26 (%Y-%m-%d)
。
为此,我使用下一个代码:
dt[, full_date := format(ISOweek2date(sub("(\d{4})(\d{2})", "\1-W\2-1", year_week)),"%Y-%m-%d")]
但是,我有以下错误:
"Error: all(is.na(weekdate) | stringr::str_detect(weekdate, kPattern)) is not TRUE"
如何通过其他方式获取完整日期?
为什么会出现此错误?
非常感谢您的帮助!
P.S。星期日或星期一可以作为一周的第一天。
您可以将工作日添加到日期并使用
as.Date(paste0(df$year_week, 1),"%Y%U%u")
#[1] "2018-01-29" "2019-09-23" "2017-11-06" "2014-01-13" "2015-03-09"
数据
df <- structure(list(year_week = c(201804L, 201938L, 201745L, 201402L,
201510L)), class = "data.frame", row.names = c(NA, -5L))
我有一个格式如下的日期(年份和周数):
year_week
201804
201938
201745
201402
201510
我需要获取格式完整的日期:2018-02-26 (%Y-%m-%d)
。
为此,我使用下一个代码:
dt[, full_date := format(ISOweek2date(sub("(\d{4})(\d{2})", "\1-W\2-1", year_week)),"%Y-%m-%d")]
但是,我有以下错误:
"Error: all(is.na(weekdate) | stringr::str_detect(weekdate, kPattern)) is not TRUE"
如何通过其他方式获取完整日期? 为什么会出现此错误?
非常感谢您的帮助!
P.S。星期日或星期一可以作为一周的第一天。
您可以将工作日添加到日期并使用
as.Date(paste0(df$year_week, 1),"%Y%U%u")
#[1] "2018-01-29" "2019-09-23" "2017-11-06" "2014-01-13" "2015-03-09"
数据
df <- structure(list(year_week = c(201804L, 201938L, 201745L, 201402L,
201510L)), class = "data.frame", row.names = c(NA, -5L))