Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R, how to replace values in a column with values of another column of another data set based on a condition?

I have to data sets, samples of which I’ve given below. I need to replace project names in target_df$project_name, in case they are present in registry_df$to_change with corresponding values in registry_df$replacement. However, the code I tried, obviously, did not deliver any result. How should it be corrected or what other way there is to achieve the desired goal?

Data sets:

target_df <- tibble::tribble(
  ~project_name,     ~sum,   
  "Mark",            "4307",     
  "Boat",            "9567",       
  "Delorean",        "5344",      
  "Parix",           "1043",
)

registry_df <- tibble::tribble(
  ~to_change,     ~replacement,   
  "Mark",            "Duck",     
  "Boat",            "Tank",       
  "Toloune",         "Bordeaux",      
  "Hunge",           "Juron",
)

Desired output of target_df :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

project_name        sum   
  "Duck"            "4307"     
  "Tank"            "9567"       
  "Delorean"        "5344"      
  "Parix"           "1043"

Code:

library(data.table)

target_df <- transform(target_df, 
                       project_name = ifelse(target_df$project_name %in% registry_df$to_change),
                       registry_df$replacement,
                       project_name
)

>Solution :

A dplyr solution. There’s probably an elegant way with less steps.

library(dplyr)

target_df <- target_df %>% 
  left_join(registry_df,  
            by = c("project_name" = "to_change")) %>% 
  mutate(replacement = ifelse(is.na(replacement), project_name, replacement)) %>% 
  select(project_name = replacement, sum)

Result:

# A tibble: 4 × 2
  project_name sum  
  <chr>        <chr>
1 Duck         4307 
2 Tank         9567 
3 Delorean     5344 
4 Parix        1043
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading