Search code examples
rdataframefunctiondplyrtidyverse

Replace wrong values in a dataframe using a dataframe of correct values


In my DATA below, some Names are misspelled. I've determined the Unique_Names and correspondingly created the Correct_Unique_Names for those unique names.

But I wonder how to insert my Correct_Unique_Names in DATA to achieve my Desired_output below?

library(tidyverse)

DATA <- read.table(header=T,text=
"Name      
Asan
Ziba       
Asan       
Asana
Farhan
Frahan
Zibaee
Ziba")

Unique_Names <- unique(DATA) %>% 
arrange()

Correct_Unique_Names <- 
  read.table(header=T,text=
"Name
 Aasan
 Zibae
 Aasan
 Farahani
 Frahani
 Zibae")

Desired_output <- read.table(header=T, text=
  "
Name      
Aasan
Zibae      
Asan       
Aasan
Farhani
Farhani
Zibae
Zibae")

Solution

  • You have some errors in your data. I'm assuming that I have fixed them correctly. The following code should work:

    DATA |>
      left_join(data.frame(Unique_Names, Correct_Unique_Names),
                by="Name") |>
      select(Name.1) |>
      rename(Name=Name.1)
    
         Name
    1   Aasan
    2   Zibae
    3   Aasan
    4   Aasan
    5 Farhani
    6 Farhani
    7   Zibae
    8   Zibae
    

    library(dplyr)