Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

Conditional matching based on two variables

$
0
0

I have a DF with similar multiple rows and columns. I want to match Name with Name1. If Name does not match Name1, I want to also try to matchName to Name3. Then if it matches, I want the value of Name2 to return as a new variable, Namevalue.

df
Name      Name1    Name2   Name3
Natalie   Shawn    Edmund  Natalie
Desmond   Desmond  James   Sue
Kylie     Kylie    Brent   Manny

Desired output

df
Name      Name1    Name2   Name3     Namevalue
Natalie   Shawn    Edmund  Natalie   Edmund
Desmond   Desmond  Tyler   Sue       Tyler
Kylie     Chris    Tam     Kylie     Tam
Leah      Chewie   Luke    Solo      Unknown

I've tried this:

Merge1$Namevalue <- ifelse(Merge1$Name %in% Merge1$Name1, Merge1$Name2, 
                    paste("Unknown"),
                    ifelse(Merge1$Name %in% Merge1$Name3, Merge1$Name2, paste("Unknown"))

Unfortunately, it doesn't work where it doesn't match properly. Any help would be appreciated.


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>