Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

date - Get days based on integer week and year in R

I have many strings like "200046". The first four digits are the year, and the last two is the number of the week per year. I'm trying to find the 7 days of the week for that week. I tried something like

date = as.Date(str, "%Y%M")

but it returns "2000-01-29" which is not the 46th week of 2000. How can I do that?

question from:https://stackoverflow.com/questions/65951897/get-days-based-on-integer-week-and-year-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add day of the week to str.

str <- '200046'
as.Date(paste0(str, 1), "%Y%U%u")
#[1] "2000-11-13"

This is 1st day (Monday) of 46th week of 2000.

Now to get all days of the week you can do :

as.Date(paste0(str, 1), "%Y%U%u") + 0:6
#[1] "2000-11-13" "2000-11-14" "2000-11-15" "2000-11-16" "2000-11-17" "2000-11-18" "2000-11-19"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...