-1

I have a data set dat with a year and a week variable (both integers). I want to combine them into a date POSIXct variable in R of the format W-YYYY.

However, I get NA values when I use the as.POSIXct function.

My code is below:

as.POSIXct(paste(dat$week, dat$year), format="%V-%Y")
lala_12
  • 131
  • 2
  • 10
  • 2
    I do not think a weeknumber can represent a posixct, since it is a period – Wimpel Jun 12 '18 at 11:49
  • 1
    The date formats require complete data. You need to atleast include a dummy weekday – Rohit Jun 12 '18 at 12:01
  • A value of class `Date` or `POSIXct` does not have a "format". A particular format will only be used when converting to character, e.g., for printing. – Uwe Jun 22 '18 at 06:11
  • There are different definitions of how to number the weeks of a year. Which one do you want to use? `%V` refers to week numbering according to ISO8601 but `%Y` to the calendar year. In ISO8601 week numbering, the year may differ from the calendar year. The ISO8601 week numbering is referred by `%G`. – Uwe Jun 22 '18 at 06:14
  • Please, see these answers for more details: [Transform year/week to date object](https://stackoverflow.com/a/45587644/3817004), [Get week starting date from weeknum and year in R](https://stackoverflow.com/a/48720354/3817004) – Uwe Jun 22 '18 at 06:18

1 Answers1

2

I suppose you'll want the first monday of the given week? If you want an other day, change the value of %u accordingly.

yr = 2018
wk = 10
as.POSIXct( paste( 1, wk, yr, sep = "-" ), format = "%u-%U-%Y" )
[1] "2018-03-12 CET"
Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • Are you aware that there are three different definitions of how to count week of the year? – Uwe Jun 22 '18 at 06:21