Ty Voss
Ty Voss

Reputation: 283

r if else based on multiple conditions

I have a dataset data1 as follows

          Group     Code
          Blue      1333
          Blue      4444
          Blue      9876
          Blue      8785
          Red       3145
          Red       8756
          Red       9745
          Red       8754

Second dataset data2 is as follows

          Id       Description
          1333     Sea Weed
          4444     Honey Roasted Peanut
          8754     Green Tea
          8756     Potato Chips
          3145     Strawberry Grahams
          8787     Arizona Ice Tea

I am trying to create a third column in my 2nd Dataset , data2 which stores

           1  - If the code is from blue Group in Data1 and matches with Id in Data2, Data1$Group = Blue && Data1$Code == Data2$Id

           2  - If the code is from Red Group in Data1 and matches with Id in Data2, Data1$Group = Red && Data1$Code == Data2$Id

           0  - If the Id in Data2 does not match the Code in Data1 , regardless of whether it is Blue or Red group.

The final dataset should look like this

          Id       Description             Result
          1333     Sea Weed                1  
          4444     Honey Roasted Peanut    1
          8754     Green Tea               2
          8756     Potato Chips            2 
          3145     Strawberry Grahams      2 
          8787     Arizona Ice Tea         0

Need some assistance

Upvotes: 4

Views: 329

Answers (1)

user295691
user295691

Reputation: 7248

Easier base R answer is to use merge

> merge(data1, data2, by.x='Code', by.y='Id', all.y=T)

  Code Group          Description
1 1333  Blue             Sea Weed
2 3145   Red   Strawberry Grahams
3 4444  Blue Honey Roasted Peanut
4 8754   Red            Green Tea
5 8756   Red         Potato Chips
6 8787  <NA>      Arizona Ice Tea

If your heart is set on using dplyr, then renaming the column is the easiest way to do this is to rename the column so that it matches the merged table

data2 %>% rename(Code=Id) %>% left_join(data1)

Upvotes: 1

Related Questions