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 alphanumeric string in R

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

Example: What I am expecting
#100001032218888example1 #nnnnaaaannnn
#1-example-2 #nnnn-aaaa-nnnn

I tried this:

  mutate(str_alpha = str_replace_all(col_name, "\\w+", "aaaa"),
         str_num_alpha = str_replace_all(str_alpha, "\\d+", "nnnn"))

I think I got the "\\w+" incorrect because it replaces the numeric part of the string too.

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

Can someone please help? Thank you.

>Solution :

Do the reverse i.e. replace first the alphabets and then the digits so

library(stringr)
str_replace_all(str_replace_all(str1, "[[:alpha:]]+", "aaaa"), "\\d+", "nnnn")

-output

[1] "#nnnnaaaannnn"   "#nnnn-aaaa-nnnn"

Or using a named vector in a single str_replace_all

str_replace_all(str1, setNames(c("aaaa", "nnnn"), c("[[:alpha:]]+", "\\d+")))
[1] "#nnnnaaaannnn"   "#nnnn-aaaa-nnnn"

data

str1 <- c("#100001032218888example1", "#1-example-2")
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