The dplyr
based solutions are more human-readable but if you want to keep using base R, this would be an alternative solution.
df1 <- data.frame(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
MONTH = c('MONTH01', 'MONTH02', 'MONTH03', 'MONTH04', 'MONTH05','MONTH06',
'MONTH07','MONTH08','MONTH09','MONTH10','MONTH11','MONTH12',
'MONTH01', 'MONTH02', 'MONTH03', 'MONTH04', 'MONTH05','MONTH06',
'MONTH07','MONTH08','MONTH09','MONTH10','MONTH11','MONTH12',
'MONTH01', 'MONTH02', 'MONTH03', 'MONTH04', 'MONTH05','MONTH06',
'MONTH07','MONTH08','MONTH09','MONTH10','MONTH11','MONTH12',
'MONTH01', 'MONTH02', 'MONTH03', 'MONTH04', 'MONTH05','MONTH06',
'MONTH07','MONTH08','MONTH09','MONTH10','MONTH11','MONTH12'),
YEAR = c(2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019))
(df1$MONTH <- paste0(df1$YEAR,substr(df1$MONTH,6,7)))
#> [1] "201801" "201802" "201803" "201804" "201805" "201806" "201807" "201808"
#> [9] "201809" "201810" "201811" "201812" "201901" "201902" "201903" "201904"
#> [17] "201905" "201906" "201907" "201908" "201909" "201910" "201911" "201912"
#> [25] "201801" "201802" "201803" "201804" "201805" "201806" "201807" "201808"
#> [33] "201809" "201810" "201811" "201812" "201901" "201902" "201903" "201904"
#> [41] "201905" "201906" "201907" "201908" "201909" "201910" "201911" "201912"
Created on 2023-01-16 with reprex v2.0.2