合并两个数据框和vlookup

时间:2019-09-19 01:58:04

标签: r

我有两个数据帧(df1,df2)。我想将它们组合起来,以便如果df1中存在一个特定样品ID的基因,我希望在相应的匹配基因列和df2中的样品ID行中为其分配“ 1”(存在)。该基因不存在于df1中,它将自动在df2中填充“ 0”(不存在),如下面的结果数据框中所示。在此方面您的帮助将非常感谢。

df1

+-----------+-----+
| Gene name | ID  |
+-----------+-----+
| A         | 100 |
| A         | 105 |
| B         | 100 |
| B         | 101 |
| B         | 103 |
| C         | 105 |
+-----------+-----+

df2

+-----+---+---+---+---+
| ID  | A | B | C | D |
+-----+---+---+---+---+
| 100 |   |   |   |   |
| 101 |   |   |   |   |
| 102 |   |   |   |   |
| 103 |   |   |   |   |
| 104 |   |   |   |   |
| 105 |   |   |   |   |
| 106 |   |   |   |   |
+-----+---+---+---+---+

结果

+-----+---+---+---+---+
| ID  | A | B | C | D |
+-----+---+---+---+---+
| 100 | 1 | 1 | 0 | 0 |
| 101 | 0 | 1 | 0 | 0 |
| 102 | 0 | 0 | 0 | 0 |
| 103 | 0 | 1 | 0 | 0 |
| 104 | 0 | 0 | 0 | 0 |
| 105 | 0 | 0 | 1 | 0 |
| 106 | 0 | 0 | 0 | 0 |
+-----+---+---+---+---+

0 个答案:

没有答案