I have a df with 2 columns where the second one represents strings that contains special characters and other characters I want to remove.
The problem
I have written a for
loop that works but only after being executed Three (03) times!
Libraries & Data
library(tidyverse)
client_id <- 1:10
client_name <- c("name5", "-name", "name--", "name-µ", "name²", "name31", "7name8", "name514", "²name8")
df <- data.frame(cbind(client_id, client_name))
Patterns to be removed
patterns <- list("-", "--", "[:digit:]", "[:cntrl:]" , "µ" , "²" , "[:punct:]")
What I have done
To remove the unwanted patterns in col 2 client_names
I have written the following for
loop:
for(ptrn in patterns) {
df <- df %>%
mutate(client_name = str_remove(df$client_name, ptrn))
print(ptrn) # progress
}
The above for
loop removes all unwanted patterns, but only after being executed Three (03) times.
How can we fix that in order to remove all unwanted patterns since the first execution?
Should I nest the above for
loop with another one in order to iterate over client_names[i]
?
Thanks