简体   繁体   English

将周。年转换为年。月-R中的日期

[英]Convert week.year to year.month - date in R

I would like to convert a date week.year ("44.2016") to year.month (2016-11) in ISO 8601 我想在ISO 8601中将日期week.year(“ 44.2016”)转换为year.month(2016-11)

I have a variable date_week <- "44.2016" 我有一个可变的date_week <- "44.2016"

I tried this : 我尝试了这个:

as.Date(date_week, format = "%U.%Y")

as.Date(date_week, format = "%W.%Y")

I have this result: "2016-02-21" 我有这个结果: "2016-02-21"

I also tried this : 我也尝试过这个:

as.Date(date_week, format = "%w.%Y")

as.Date(date_week, format = "%w.%Y")

result : NA 结果:不NA

I would like to have this RESULT : "2016-11" 我想要这个结果: "2016-11"

Thanks for your help ! 谢谢你的帮助 !

Note that you need to specify a day since a week can imply two consecutive months. 请注意,您需要指定一天,因为一周可能意味着连续两个月。 Try this: 尝试这个:

as.Date(paste("1", date_week, sep = "."), format = "%w.%W.%Y") # 1st day (monday 31 oct 2016)
as.Date(paste("2", date_week, sep = "."), format = "%w.%W.%Y") # 2nd day (Tuesday 01 nov 2016)
# new format 
format(as.Date(paste("1", date_week, sep = "."), format = "%w.%W.%Y"), "%Y-%m")
# [1] "2016-10"
format(as.Date(paste("2", date_week, sep = "."), format = "%w.%W.%Y"), "%Y-%m")
# [1] "2016-11"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM