Home > front end >  Remove numbers from strings that appears last
Remove numbers from strings that appears last

Time:01-10

Is there a way to remove numbers from the string at appears only at the last. Example

df
Col A
asdf sfsfsd54 sdfsdfsdf sdfsdfsf654
sfs sfsa5dfgdf sf54 sfsfsgg98
sfs fsdgfdsgd gdfg8

Expected output

df
Col A                                       ColB
asdf sfsfsd54 sdfsdfsdf sdfsdfsf654       asdf sfsfsd54 sdfsdfsdf sdfsdfsf
sfs sfsa5dfgdf sf54 sfsfsgg98             sfs sfsa5dfgdf sf54 sfsfsgg
sfs fsdgfdsgd gdfg8                       sfs fsdgfdsgd gdfg

Raw data

structure(list(ColA = c("asdf sfsfsd54 sdfsdfsdf sdfsdfsf654", 
"sfs sfsa5dfgdf sf54 sfsfsgg98", "sfs fsdgfdsgd gdfg8")), class = "data.frame", row.names = c(NA, 
-3L))

CodePudding user response:

This can be done via: gsub:

x <- c("test123", "test12", "test1")
gsub("[0-9] $", "", x)

Edit - using your data:

df <- structure(list(ColA = c("asdf sfsfsd54 sdfsdfsdf sdfsdfsf654", 
                             "sfs sfsa5dfgdf sf54 sfsfsgg98", "sfs fsdgfdsgd gdfg8")), class = "data.frame", row.names = c(NA, 
                                                                                                                           -3L))
df$ColB <- gsub("[0-9] $", "", df$ColA)

> df
ColA                             ColB
1 asdf sfsfsd54 sdfsdfsdf sdfsdfsf654 asdf sfsfsd54 sdfsdfsdf sdfsdfsf
2       sfs sfsa5dfgdf sf54 sfsfsgg98      sfs sfsa5dfgdf sf54 sfsfsgg
3                 sfs fsdgfdsgd gdfg8               sfs fsdgfdsgd gdfg

CodePudding user response:

Here is an alternative approach using str_remove:

library(dplyr)
library(stringr)
df %>% 
  mutate(ColB = str_remove(ColA, "\\d $"))
                                 ColA                             ColB
1 asdf sfsfsd54 sdfsdfsdf sdfsdfsf654 asdf sfsfsd54 sdfsdfsdf sdfsdfsf
2       sfs sfsa5dfgdf sf54 sfsfsgg98      sfs sfsa5dfgdf sf54 sfsfsgg
3                 sfs fsdgfdsgd gdfg8               sfs fsdgfdsgd gdfg
  •  Tags:  
  • Related