【问题标题】:Match multiple condition on large dataframe in R在R中的大型数据帧上匹配多个条件
【发布时间】:2018-04-03 17:09:04
【问题描述】:

我在下面提到了两个数据框:

DF_1

Val1           Val2
COPPAR Ert     Metal          
Bittar Gourd   vegetble
Blackbery d    Fruite

DF_2

Val4           Val5        Type
Copper         Metal       A-I   
Bitter Gourd   Vegetable   B-II
Blackberry     Fruit       C-III

我在Val1Val2 中的DF_1 中有一些错误(Val1Val2 中的类似字符串在拼写上不同)并且在DF_2 中有正确的列表。只想将 DF_1 的 Val1 与 DF_2 的 Val4 匹配,并基于正确的值 (New_Val1) 我想要输出数据帧中的 New_Val2Type 中的 Val5

输出数据框:

Val1           Val2      New_Val1       New_Val2    Type
COPPAR         Metal     Copper Ert     Metal       A-I      
Bittar Gourd   vegetble  Bitter Gourd   Vegetable   B-II
Blackbery      Fruite    Blackberry     Fruit       C-III

【问题讨论】:

  • 你可以使用 soundex
  • @Wen 那是什么……??我没用过。
  • @Wen 谢谢,您能否建议我如何在问题中提到的条件下在我的大型数据框中使用它。
  • 是的,添加一个答案

标签: r dataframe dplyr tidyr


【解决方案1】:

这是基于 soundex

library(phonics)

df1['match1']=soundex(df1$Val1)
df1['match2']=soundex(df1$Val2)
df2['match1']=soundex(df2$Val4)
df2['match2']=soundex(df2$Val5)
merge(df1,df2,by=c('match1','match2'))
  match1 match2         Val1     Val2         Val4      Val5  Type
1   B360   V231 Bittar Gourd vegetble Bitter Gourd Vegetable  B-II
2   B421   F630  Blackbery d   Fruite   Blackberry     Fruit C-III
3   C160   M340   COPPAR Ert    Metal       Copper     Metal   A-I

【讨论】:

    最近更新 更多