r if else基于多个条件

时间:2015-02-20 00:15:05

标签: r if-statement dplyr

我有一个数据集data1如下

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

第二个数据集data2如下

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

我正在尝试在第二个数据集中创建第三列,data2存储

           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.

最终数据集应如下所示

          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

需要一些帮助

1 个答案:

答案 0 :(得分:1)

更简单的基础R答案是使用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

如果您的心脏被设置为使用dplyr,那么重命名该列是最简单的方法,即重命名该列以使其与合并的表匹配

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