r - R:如何从年周中提取完整日期?
问题描述
我有以下格式的日期(年和周数):
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"
如何以另一种方式获得完整日期?为什么我有这个错误?
非常感谢您的帮助!
PS 星期日或星期一可以用作一周的第一天。
解决方案
您可以将工作日添加到日期并使用
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))
推荐阅读
- r - 如果两列中的任何一列具有指定值,则过滤行
- django - 如何在 django 中使用 getlist()
- botframework - 在 Composer 中使用自适应卡作为输入表单的任何示例?
- java - 如何正确初始化HashMap
- android - 如何在 Android API 级别 28 中使用 addOverrideApn() 方法?
- r - 分组,然后在日期时间超过某个时间时创建一个“中断”,在原始分组列(R,dplyr)中创建一个新值
- javascript - 按日期对 JSON 对象数组进行排序
- python - python的set函数的异常行为
- node.js - 在 react.js 中附加表单请求时,请求正文在服务器 node.js 中显示未定义
- mysql - SQL 计算可用性百分比