0

I have a dataframe field with as date field that is a factor with the following format "2019 W01", "2019 W02", "2019 W03". how can I transform it into a date field ?

1 Answers1

0

It's probably best to first clean the dates from strings using gsub, then add 1 for first day of the week and convert as.Date using format="%Y%U%u" option.

x <- c("2019 W01", "2019 W02", "2019 W03")
as.Date(paste0(gsub("\\D", "", x), 1), format="%Y%U%u")
# [1] "2019-01-07" "2019-01-14" "2019-01-21" 

This answer bay be considered as a special case of an older relevant answer.

jay.sf
  • 60,139
  • 8
  • 53
  • 110