我有一个数据集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
需要一些帮助
最佳答案
更简单的基本 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)
关于r if else 基于多个条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28619528/