Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Can someone please help? Thank you.

like image 837
user2845095 Avatar asked May 18 '26 08:05

user2845095


1 Answers

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")
like image 122
akrun Avatar answered May 20 '26 01:05

akrun