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

Replacing numeric string of varying lengths with characters in R

I have a dataset with alphanumeric strings. I am trying to replace the numeric part of the string with "nnnn". The length of the numeric part does not matter.

Example: What I am expecting
#100001032218888example1 #nnnnexamplennnn
#1example2 #nnnnexamplennnn

>Solution :

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

We can use gsub() here as follows:

x <- c("#100001032218888example1", "#1example2")
output <- gsub("\\d+", "nnnn", x)
output

[1] "#nnnnexamplennnn" "#nnnnexamplennnn"
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