I have a partial date string
partial <- "UN-Jan-2022"
My desired output
> parital
Jan
I'm trying to do this by using gsub, I can remove "UN-" or -"2022" but not sure how to remove both in one gsub
gsub("UN-", "", partial)
Jan-2022
gsub("-2022", "", partial)
UN-Jan
CodePudding user response:
Another gsub
option which extracts everything between both "-" like this:
partial <- "UN-Jan-2022"
gsub(".*-(.*)\\-.*", "\\1", partial)
#> [1] "Jan"
Created on 2022-07-27 by the reprex package (v2.0.1)
CodePudding user response:
We could convert to Date
format and then use format
format(as.Date(paste0(partial, '-01'), 'UN-%b-%Y-%d'), '%b')
[1] "Jan"
CodePudding user response:
Another possible solution, based on stringr::str_match
and regex capture groups:
stringr::str_match(partial, ".*-(.*)-.*")[2]
#> [1] "Jan"