2

Consider that I have df like this:

df <- tribble(
  ~Country, 
  "Bangladesh", 
  "India", 
  "Nigeria", 
  "China", 
  "France", 
  "Canada", 
)

and I only wanted to change some names not all. For instance, only China and France. I tried the below code but it repalces NA for the rest.

shortNames = c("China" = "CHN", "France" = "FR")
df$Country <- shortNames[df$Country]
rez
  • 290
  • 2
  • 12
  • 1
    Try running `shortNames["China"]` and then `shortNames["India"]` and see if you can work out what's going on. – SamR Apr 03 '23 at 10:01
  • 1
    `df$Country[match(names(shortNames), df$Country)] <- shortNames` – GKi Apr 03 '23 at 10:03
  • 2
    or: `i <- df$Country %in% names(shortNames); df$Country[i] <- shortNames[df$Country[i]]` – GKi Apr 03 '23 at 10:05

2 Answers2

2

You need exclude the names not in shortNames.

i <- df$Country %in% names(shortNames)
df$Country[i] <- shortNames[df$Country[i]]

df
#  Country   
#  <chr>     
#1 Bangladesh
#2 India     
#3 Nigeria   
#4 CHN       
#5 FR        
#6 Canada    
GKi
  • 37,245
  • 2
  • 26
  • 48
2

You may use stringr::str_replace_all which takes named vector to replace values.

df$Country <- stringr::str_replace_all(df$Country, shortNames)
df

# A tibble: 6 × 1
#  Country   
#  <chr>     
#1 Bangladesh
#2 India     
#3 Nigeria   
#4 CHN       
#5 FR        
#6 Canada    

If you want to use it in dplyr pipes, you can do -

library(dplyr)
library(stringr)

df %>% mutate(Country = str_replace_all(Country, shortNames))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Thanks. That is a very concise way to the problem. But for now I prefer the base R answer provided by @GKi. – rez Apr 03 '23 at 10:24