1

I have times in the format YYYY.week:

x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01", "2017.02", "2017.03", "2017.04", "2017.05")

Is there a way to create the convert it to date variables like this (date is not true, but that's the desired format):

as.Date(x[1])
"2017-02-01"
Henrik
  • 61,039
  • 13
  • 131
  • 152
Data Mastery
  • 989
  • 3
  • 11
  • 31

3 Answers3

2

One lubridate option could be:

ymd(paste0(substr(x, 1, 4), "-01", "-01"))+weeks(as.numeric(substr(x, nchar(x)-1, nchar(x)))-1)

 [1] "2016-12-02" "2016-12-09" "2016-12-16" "2016-12-23" "2017-01-01"

Sample data:

x <- c("2016.49", "2016.50", "2016.51", "2016.52", "2017.01")
tmfmnk
  • 36,341
  • 4
  • 40
  • 53
2

If you append a weekday then as.Date will work:

x <- c("2016.49", "2016.50", "2016.51")

as.Date(paste0(x, ".1"), "%Y.%U.%u")

[1] "2016-12-05" "2016-12-12" "2016-12-19"
Aron
  • 2,220
  • 7
  • 12
1

One more lubridate option

parse_date_time(paste0(x,'.Mon'),'Y/W/a')
[1] "2016-12-05 UTC" "2016-12-12 UTC" "2016-12-19 UTC" "2016-12-26 UTC" "2017-01-02 UTC"
Yuriy Barvinchenko
  • 1,303
  • 1
  • 8
  • 15