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
  • 65,555
  • 14
  • 143
  • 159
Data Mastery
  • 1,555
  • 4
  • 18
  • 60

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
  • 38,881
  • 4
  • 47
  • 67
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 Strandberg
  • 3,040
  • 9
  • 15
2

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,465
  • 1
  • 12
  • 17