jeudi 19 février 2015

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


Aucun commentaire:

Enregistrer un commentaire