r - 通过字符串匹配从其他数据帧中查找特定值
问题描述
我在下面提到了两个数据框:
DF1
ID Value
AL-1 Adf "& ert
AL-2 new '? rti
AL-3 oll- drt/
AL-4 plr -rte-
DF2
Value Type
Adf & ert AA
new rti AA
oll-drt& AB
plr-rte AC
所需输出:
ID Value Type
AL-1 Adf "& ert AA
AL-2 new '? rti AA
AL-3 oll- drt/ AB
AL-4 plr -rte- AC
我想将 Value 列中 DF1 的字符与 DF2 Value 列的字符进行匹配,并导出Type
.
尝试使用 phonics 库但还没有完成,这里需要一些建议。
解决方案
您可以使用该adist
函数来获取df1$Value
与df2$Value
. 然后您可以选择距离最短的那个(因此将优先考虑完全匹配):
library(utils)
df1$Type <- df2$Type[apply(adist(df1$Value, df2$Value), 1, which.min)]
df1
# ID Value Type
#1 AL-1 Adf "& ert AA
#2 AL-2 new '? rti AA
#3 AL-3 oll- drt/ AB
#4 AL-4 plr -rte- AC
推荐阅读
- python - 为什么取消 qfiledialog 有错误?
- google-apps-script - 使用 Google Apps 脚本将 Google 幻灯片演示文稿下载为 PowerPoint 文档?
- git - 以编程方式显示合并期间使用的所有 git rerere 分辨率
- oracle - DBMS_SCHEDULING 重复间隔 - 半小时,9-5,周一至周五
- python - Django 对过去 10 周内每周的值求和
- opencv - 使用内部和外部相机参数进行图像匹配
- flutter - How to change language of system clipboard text (Copy, Paste etc.,) to Chinese in Flutter?
- sql-server - 有没有办法回滚事务中的特定保存点?
- python - 美丽的汤从 Id 中获取文本
- reactjs - 反应纯组件