将星期数转换为日期 [英] Convert week number to date
问题描述
我在R中有一个数据框,其中有要转换为日期的一年中的星期.我知道我必须选择一年中的一天,因此我将这些值固定为2014和1.将其转换为日期似乎很简单:
I have a data frame in R with the week of the year that I would like to convert to a date. I know I have to pick a year and a day of the week so I am fixing those values at 2014 and 1. Converting this to a date seems simple:
as.Date(paste(2014,df$Week,1,sep=""),"%Y%U%u")
但是,仅当Week大于9时,此代码才有效.Week 1-9返回NA.如果将星期更改为01,02,03 ...,它仍然返回NA.
But this code only works if week is greater than 9. Week 1 - 9 returns NA. If I change the week to 01,02,03... it still returns NA.
有人看到我想念的东西吗?
Anyone see what I am missing?
推荐答案
as.Date
调用1到9作为NA,因为它期望星期数为两位数,并且无法正确解析.
as.Date
is calling the 1 to 9 as NA as it is expects two digits for the week number and can't properly parse it.
要修复此问题,请添加一些内容-拆分内容:
To fix it, add in some - to split things up:
as.Date(paste(2014, df$Week, 1, sep="-"), "%Y-%U-%u")
这篇关于将星期数转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!