Remove characters right after special character R dplyr

Viewed 193

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'))
4 Answers

You can use -

sub('[-:]\\w+|:', '', test$label, perl = TRUE)

#[1] "BR Santa Catarina" "BR São Paulo"      "MM Nay Pyi Taw"   
#[4] "PS Tulkarm"        "HK Yau Tsim Mong"  "JO Ajlūn"         
#[7] "RU Altajskij kraj" "RW Kibuye"         "EG Kafr el-Sheikh"

Using tidyverse -

library(dplyr)
library(stringr)

test %>% mutate(label1 = str_remove(label, '[-:]\\w+|:'))

It removes a word followed by - or :, since we are using sub/str_remove it will remove only the first occurrence of it.

another method:

    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'))

    a = strsplit(test$label, ' ')

    for(i in 1:length(a)){
      a[[i]][1]= substr(a[[i]][1],1,2)
      a[[i]]= paste(a[[i]], collapse = ' ')
    }
 
   final = data.frame( label=unlist(a))

   final

output :

              label
1 BR Santa Catarina
2      BR São Paulo
3    MM Nay Pyi Taw
4        PS Tulkarm
5  HK Yau Tsim Mong
6          JO Ajlūn
7 RU Altajskij kraj
8         RW Kibuye
9 EG Kafr el-Sheikh

try gsub

library(dplyr)
test |> mutate(label = gsub("(:|-)(.*?)\\s"," ",label))

We can use str_remove

library(stringr)
str_remove(test$label, "-\\w+")
[1] "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"        
[9] "EG Kafr el-Sheikh"
Related