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 :
We can use gsub() here as follows:
x <- c("#100001032218888example1", "#1example2")
output <- gsub("\\d+", "nnnn", x)
output
[1] "#nnnnexamplennnn" "#nnnnexamplennnn"