Dr.E77
Dr.E77

Reputation: 107

Matching based on conditional data

I have two Dfs with multiple rows and columns. I want to see if Df1$Name matches Df2$Name. If it matches, I want it to take match value and create a new variable in DF3. But if it doesn't match, I want to paste the value from Df1. The issue is that Df1 has 270 observations and Df2 has 277.

See example:

Df1
       Name      
       Natalie   
       Desmond,James   
       Kylie     

Df2
        Name      
         <Na>
        Desmond,James  
        <Na>
 Df3    
        Merged_name
        Natalie
        Desomond,James
        Kylie

I've tried:

Df3$Merged_name <- ifelse(Df1Name %in% Df$Name 
              & !is.na(Df2$Name), Df1$Name 
                 , Df2$Name)

I get an error saying that the longer object length is not a multiple of shorter object length which I'm assuming is due to the varying observations. Do I have to separate rows that have more than one name in it(i.e. separate_rows())? If so, how do I re-merge back together?

Upvotes: 0

Views: 99

Answers (1)

Nareman Darwish
Nareman Darwish

Reputation: 1261

You can use cbind.fill function which accepts binding of columns given different row numbers and then you do the conditional scenario you gave;

library(dplyr)
library(rowr)

Df1 <-
  data.frame(
    Name = c("Natalie", "Desmond,James", "Kylie"),
    stringsAsFactors = FALSE
  )

Df2 <-
  data.frame(
    Name = c(NA_character_, "Desmond,James", NA_character_, "Test"),
    stringsAsFactors = FALSE
  )

# Binding data by column and renaming similar column names
cbind.fill(Df1 %>% rename(Name1 = Name), Df2 %>% rename(Name2 = Name), fill = NA) %>%
  mutate(Name = coalesce(Name2, Name1)) %>% # Conditional logic given
  select(Name)

# Name
# Natalie
# Desmond,James
# Kylie
# Name1

Upvotes: 2

Related Questions