Title: Can't reduce whitespaces, remove accent, and replace words in R
Tags: r;stringr
Question: I have the following data frame :
```structure(list(matching_var = c(&quot;BD Mymensingh&quot;, &quot;CN Nei Mongol &quot;, &quot;EC Los Ríos&quot;, &quot;MY  Johor&quot;, &quot;MY  Kedah&quot;, &quot;MY  Kelantan&quot;, &quot;MY Negeri Sembilan&quot;, &quot;RU Amurskaja oblast&quot;)), row.names = c(44L,174L, 259L, 694L, 695L, 696L, 700L, 1029L), class = &quot;data.frame&quot;)
```
I would like it to become :
```structure(list(matching_var = c(&quot;BD Mymensingh&quot;, &quot;CN Nei Mongol&quot;, &quot;EC Los Rios&quot;, &quot;MY Johor&quot;, &quot;MY Kedah&quot;, &quot;MY Kelantan&quot;, &quot;MY Negeri Sembilan&quot;, &quot;RU Amur&quot;)), row.names = c(44L, 174L, 259L, 694L, 695L, 696L, 700L, 1029L), class = &quot;data.frame&quot;)
```
I tried the following code, but it does not work :
```library(dplyr)
library(stringr)

data <- data %&gt;%
  mutate(matching_var = str_replace(matching_var, &quot;MY  Johor&quot;, &quot;MY Johor&quot;)) %&gt;% 
  mutate(matching_var = str_replace(matching_var, &quot;Los Ríos&quot;, &quot;Los Rios&quot;))%&gt;% 
  mutate(matching_var = str_replace(matching_var, &quot;MY  Kedah&quot;, &quot;MY Kedah&quot;))%&gt;% 
  mutate(matching_var = str_replace(matching_var, &quot;MY  Kelantan&quot;, &quot;MY Kelantan &quot;))%&gt;% 
  mutate(matching_var = str_replace(matching_var, &quot;Amurskaja oblast&quot;, &quot;Amur&quot;))
```
I Also tried this for whitespaces but still nothing :
```data$matching_var <-str_replace_all(data$matching_var, fixed(&quot;  &quot;), &quot; &quot;)
```
The output looks exactly like the input, I can't figure out why. Thanks for your help.


Here is the accepted answer: 
If you're going to do replacement with fixed strings, one option is to ```merge```/```left_join``` a conversion table:
```conversions <- tribble(
~fm,               ~ to,
&quot;MY  Johor&quot;        , &quot;MY Johor&quot;,
&quot;Los Ríos&quot;         , &quot;Los Rios&quot;,
&quot;MY  Kedah&quot;        , &quot;MY Kedah&quot;,
&quot;MY  Kelantan&quot;     , &quot;MY Kelantan &quot;,
&quot;Amurskaja oblast&quot; , &quot;Amur&quot;)

left_join(data, conversions, by = c(&quot;matching_var&quot; = &quot;fm&quot;)) %&gt;%
  mutate(
    new_matching_var = coalesce(to, matching_var)
  ) %&gt;%
  select(-to)
#          matching_var    new_matching_var
# 1       BD Mymensingh       BD Mymensingh
# 2      CN Nei Mongol       CN Nei Mongol 
# 3         EC Los Ríos         EC Los Ríos
# 4           MY  Johor            MY Johor
# 5           MY  Kedah            MY Kedah
# 6        MY  Kelantan        MY Kelantan 
# 7  MY Negeri Sembilan  MY Negeri Sembilan
# 8 RU Amurskaja oblast RU Amurskaja oblast
```
(Note that the ```&quot;MY Kelantan &quot;``` has a trailing space that you added to your own code.)

Another option is to use ```conversions``` and ```match```, without the merge/join:
```data %&gt;%
  mutate(
    ind = match(matching_var, conversions$fm), 
    new_matching_var = if_else(is.na(ind), 
    matching_var, conversions$to[ind])
  ) %&gt;%
  select(-ind)
#             matching_var    new_matching_var
# 44         BD Mymensingh       BD Mymensingh
# 174       CN Nei Mongol       CN Nei Mongol 
# 259          EC Los Ríos         EC Los Ríos
# 694            MY  Johor            MY Johor
# 695            MY  Kedah            MY Kedah
# 696         MY  Kelantan        MY Kelantan 
# 700   MY Negeri Sembilan  MY Negeri Sembilan
# 1029 RU Amurskaja oblast RU Amurskaja oblast
```
While this can be done with a named vector, one reason I recommend this is that it is easily maintained (e.g., as a CSV in a directory; you can use Excel or Calc to edit/maintain the list of conversions you want).

For the blank-space removal, one can use ```trimws``` to remove extra spaces from the beginning or end of the strings. I'll continue
```data %&gt;%
  mutate(
    ind = match(matching_var, conversions$fm), 
    new_matching_var = if_else(is.na(ind), 
    matching_var, conversions$to[ind])
  ) %&gt;%
  select(-ind) %&gt;%
  mutate(new_matching_var2 = trimws(new_matching_var))
#             matching_var    new_matching_var   new_matching_var2
# 44         BD Mymensingh       BD Mymensingh       BD Mymensingh
# 174       CN Nei Mongol       CN Nei Mongol        CN Nei Mongol
# 259          EC Los Ríos         EC Los Ríos         EC Los Ríos
# 694            MY  Johor            MY Johor            MY Johor
# 695            MY  Kedah            MY Kedah            MY Kedah
# 696         MY  Kelantan        MY Kelantan          MY Kelantan
# 700   MY Negeri Sembilan  MY Negeri Sembilan  MY Negeri Sembilan
# 1029 RU Amurskaja oblast RU Amurskaja oblast RU Amurskaja oblast
```
though I don't know what didn't work with ```str_replace_all(data$matching_var, fixed(&quot;  &quot;), &quot; &quot;)```, that did remove all of the literal mid-string ```&quot;  &quot;``` that I saw (well, at this point, there are no double-spaces remaining, but it would have).


You can replace all of that perhaps with a simpler two-step cleanup, assuming that you don't need to otherwise change values:
```data %&gt;%
  mutate(
    new_matching_var = trimws(str_replace(matching_var, fixed(&quot;  &quot;), &quot; &quot;))
  )
#             matching_var    new_matching_var
# 44         BD Mymensingh       BD Mymensingh
# 174       CN Nei Mongol        CN Nei Mongol
# 259          EC Los Ríos         EC Los Ríos
# 694            MY  Johor            MY Johor
# 695            MY  Kedah            MY Kedah
# 696         MY  Kelantan         MY Kelantan
# 700   MY Negeri Sembilan  MY Negeri Sembilan
# 1029 RU Amurskaja oblast RU Amurskaja oblast
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:08/18/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><stringr>', 'Title': "Can't reduce whitespaces, remove accent, and replace words in R", 'ViewCount': 44, 'id': 68836158, 'user_AboutMe': '', 'user_AccountId': 22265077.0, 'user_DisplayName': 'Tim2009', 'user_Reputation': 37.0, 'user_Views': 8.0, 'user_id': 16495989.0}}