首页 > 解决方案 > 逐行匹配数据框两列的值

问题描述

我有一个像这样的数据框:

| Employee | Expense_Type    | Default_Expense | Amount |   |
|----------|-----------------|-----------------|--------|---|
| John     | Airfare         | Airfare         | 1000   |   |
| David    | Hotel_Tax       | Hotel           | 50     |   |
| Nancy    | Miscellaneous   | Undefined       | 500    |   |
| Mike     | Individual_Meal | Individual_Meal | 75     |   |
| Jenny    | Airline_tax     | Airfare         | 125    |   |

我想逐行比较“Expense_Type”和“Default_Expense”列并生成一个新列来粘贴不匹配的值。例如,从上表中,我们可以看到第 2、3 和 5 行不匹配,因为 hotel_tax 与 hotel 不同,miscellaneous 与 undefined 不同,airline_tax 与 airfare 不同。

我尝试研究遇到以下解决方案时提到的不同问题: df2$Expense_Type[!(df2$Expense_Type %in% df2$Default_Expense)] 但这似乎对我不起作用。

标签: rmatching

解决方案


%in%运算符不会比较每一行。使用==运算符 do 来做到这一点。这会将每一行转换为可用于过滤的布尔值:-)

df2[df2$Expense_Type == df2$Default_Expense, ]

对于值:

df$newcol <- ifelse(df2$Expense_Type == df2$Default_Expense, "Correct", "Wrong")

推荐阅读