Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove numbers from strings that appears last

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading