R, if another depends on many conditions

I have dataset1 data as follows

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

The second data set2 information 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 data set 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

Help is needed

+4
source share
1 answer

Easier to use basic R-response 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 to use dplyr, then renaming a column is the easiest way to do this - rename the column so that it matches the joined table

data2 %>% rename(Code=Id) %>% left_join(data1)
+1
source

All Articles