Home > Net >  lubridate convert format_ISO8601 year-Wweek (2022-W01)
lubridate convert format_ISO8601 year-Wweek (2022-W01)

Time:01-06

I scraped data from RKI. In a table the dates are transmitted ISO8601 compliant. The goal is to visualize later the data with ggplot in R. Unfortunately ggplot can not handle this format. Example from the Date Column.

library(lubridate)

w <- paste("2021-W", 46:52, sep = "")
w1 <-c("2022-W01")
ww <-c(w,w1)

df_ww<- format_ISO8601(as.Date(ww,format = "%YYYY-W%w“))

str(ww)
 chr [1:8] "2021-W46" "2021-W47" "2021-W48" "2021-W49" "2021-W50" "2021-W51" "2021-W52" "2022-W01“

df_ww
[1] NA NA NA NA NA NA NA NA

Any ideas to fix this?

CodePudding user response:

Try:

df_ww <- as.Date(paste0(gsub("\\D", "", ww), 1), format="%Y%U%u")

Output:

> df_ww
[1] "2021-11-15" "2021-11-22" "2021-11-29" "2021-12-06" "2021-12-13" "2021-12-20" "2021-12-27" "2022-01-03"

Update:

Ok, what about:

df_ww <- gsub("W", "", ww)

Output:

[1] "2021-46" "2021-47" "2021-48" "2021-49" "2021-50" "2021-51" "2021-52" "2022-01"
  •  Tags:  
  • Related