Reputation: 31
Hi i need to pass from character to date format My date in chr format is like : "2020-W14" so i need to pass it to date format
I m trying this with lubridate package;:
year_week="2020-W14"
date=as_date(year_week,format="%Y-W%W")
I get this date from this chunk of code but it is incorrect
2020-01-16
Any idea? thanks
Upvotes: 0
Views: 55
Reputation: 31
library(ISOweek)
year_week="2020-W14"
ISOweek2date(paste0(year_week, "-1")) #add a reference day
#> [1] "2020-03-30"
Upvotes: 0
Reputation: 616
lubridate
stores dates in days, so expects to also get a weekday.
library(lubridate)
year_week <- "2020-W14-0" # Sunday of 14th week
date <- as_date(year_week,format="%Y-W%W-%w")
## "2020-04-05"
Upvotes: 1
Reputation: 12699
You could try using the ISOweek package:
library(ISOweek)
year_week="2020-W14"
ISOweek2date(paste0(year_week, "-1")) #add a reference day
#> [1] "2020-03-30"
See Transform year/week to date object for a detailed explanation.
Created on 2021-01-16 by the reprex package (v0.3.0)
Upvotes: 2