I am trying to remove a few letters after a special character (either : or -).
I have tried things like this but the result is not satisfactory:
test2 <- test %>%
mutate(p1 = substr(label, 1, 2))
mutate(p2 = sub("^[^-*]","",label))
I was thinking doing an ifelse methodology. If after the two first letters no - and no :then leave it as it is. If after the first two letters there is a - then remove this - and the letters right next to it (either one, two or three before the space). If after the first two letters there is :then remove it and leave it as it is.
test <- data.frame(label=c('BR-SC Santa Catarina',
'BR-SP São Paulo',
'MM-18 Nay Pyi Taw',
'PS-TKM Tulkarm',
'HK Yau Tsim Mong',
'JO: Ajlūn',
'RU-ALT Altajskij kraj',
'RW-F Kibuye',
'EG-KFS Kafr el-Sheikh'))
final <- data.frame(label=c('BR Santa Catarina',
'BR São Paulo',
'MM Nay Pyi Taw',
'PS Tulkarm',
'HK Yau Tsim Mong',
'JO Ajlūn',
'RU Altajskij kraj',
'RW Kibuye',
'EG Kafr el-Sheikh'))