[英]R as.Date() convert year and week number to date
I'm running into trouble converting a year + month string containing week number 53 using the as.Date() function in R. 我在使用R中的as.Date()函数转换包含第53周的年+月字符串时遇到麻烦。
The code works for the top example for week number 52 but returns NA for the bottom example for week number 53. 该代码适用于第52周的顶部示例,但返回适用于第53周的底部示例的NA。
a <- "2017521"
as.Date(a, '%Y%W%u')
"2017-12-25"
b <- "2017531"
as.Date(b, '%Y%W%u')
NA
You're getting NA
for b <- "2017531"
because you're trying to reference a date that did not exist. 您正在得到
b <- "2017531"
NA
,因为您尝试引用的日期不存在。
This has to do with the way you formatted your date, and the way the calendar is initiated. 这与格式化日期的方式以及日历的启动方式有关。
%W
refers to the numerical week 00-53 %W
表示数字星期00-53
%u
refers to the day of the week 1-7 Monday is 1 %u
指的是星期1-7,星期一是1
b <- "2017531"
as.Date(b, '%Y%W%u')
# [1] NA
Week 53 day 1 would refer to Monday of the 53rd week. 第53周的第1天指第53周的星期一。 But the only day of the week that occurred on the 53rd week of 2017 was Sunday.
但是,2017年第53周发生的一周中的唯一一天是星期日。
c <- "2017537"
as.Date(a, '%Y%W%u')
# [1] "2017-12-31"
You can further confirm this by checking the date Saturday of week 52: 您可以通过检查第52周星期六的日期来进一步确认:
d <- "2017526"
as.Date(a, '%Y%W%u')
# [1] "2017-12-30"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.