I tried to make a recognized timestamp out of this date:
a <- VN 20190307
a <-gsub("\\D", "", a) ##Removed VN
as.POSIXct(a, format="%Y-%m-%d")
However, i get a NA. The outcome should be something like 2019-03-07
Side note: This is only a part of a confidential dataset
CodePudding user response:
If the value "VN" is fixed you can use as.Date as -
a <- "VN 20190307"
as.Date(a, 'VN %Y%m%d')
#[1] "2019-03-07"
If "VN" is not fixed you can continue your approach with gsub and then use as.Date.
as.Date(gsub('\\D', '', a), '%Y%m%d')
#[1] "2019-03-07"
CodePudding user response:
Another way is to use directly ymd function from lubridate package:
a <- 'VN 20190307'
lubridate::ymd(a)
#"2019-03-07"
