r - 逐行匹配数据框两列的值
问题描述
我有一个像这样的数据框:
| 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)]
但这似乎对我不起作用。
解决方案
%in%
运算符不会比较每一行。使用==
运算符 do 来做到这一点。这会将每一行转换为可用于过滤的布尔值:-)
df2[df2$Expense_Type == df2$Default_Expense, ]
对于值:
df$newcol <- ifelse(df2$Expense_Type == df2$Default_Expense, "Correct", "Wrong")
推荐阅读
- go - 如何使用适用于 Windows 的 GitLab 管道进行构建
- c# - 基于 Specflow 目录中的测试 json 文件创建的不必要的 .feature.cs 文件
- java - 如何设置多语言文本转语音
- forms - 使用表单发布值并显示前端
- python-3.x - 读取 csv 文件时出错,逗号分隔文件大小 65MB
- scala - 使用返回 Future 的二元运算折叠序列
- java - PDF 保存在 Oracle DB 中:错误 - java.sql.SQLException:数据大小大于此类型的最大大小:71365
- node.js - Type Error: Cannot read property 'hasListCollectionsCommand' of null
- php - 在 laravel 选择查询中获取附加数据
- python - 是否有来自 mysql 的任何 python 代码允许我选择第一行观察结果?