We can get the digits with parse_number and then use ymd with truncated to convert to Date class. If needed to change the format to month-Year, then use format

library(dplyr)
library(lubridate)
df %>% 
   mutate(date = format(ymd(readr::parse_number(str), truncated = 2), '%m-%Y'))
#        str    date
#1 ABC202003 03-2020
#2 DEF202004 04-2020

if it needs to be Date class, remove the format

df %>% 
       mutate(date = ymd(readr::parse_number(str), truncated = 2))
#        str       date
#1 ABC202003 2020-03-01
#2 DEF202004 2020-04-01

data

df <- structure(list(str = c("ABC202003", "DEF202004")), 
class = "data.frame", row.names = c(NA, 
-2L))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top