0

I have data that has a columns for year, month, week. How can I convert this to a date with lubridate. The examples in the documentation use year, month, and day.

lets assume the following data

year month week
2017  1     1
2017  2     35
2017  3     50

how can I use. makedate() with the above data?

Alex
  • 2,603
  • 4
  • 40
  • 73
  • 2
    You provide conflicting information. Week 35 will **never** be in month 2. Please edit your input data. – CPak Sep 13 '17 at 21:36

1 Answers1

2

A potential solution using base POSIXct.

df$complete_date <- as.Date(paste(df$year, df$week, 1, sep="-"), "%Y-%U-%u")

Does this produce the desired result?

  year month week complete_date
1 2017     1    1 2017-01-02
2 2017     2   35 2017-08-28
3 2017     3   50 2017-12-11
D.sen
  • 938
  • 5
  • 14
  • 1
    `%u` refers to the day of the week that you'd like to recognize as the 'start'. Monday = 1, Sunday = 7. So if you want week 35 to convert to `2017-08-27` rather than `2017-08-28` then change 1 to 7. Check out the following link for translation of symbols. https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html – D.sen Sep 13 '17 at 21:22