我遇到以下與匹配不同數據幀有關的問題。R - 匹配數據幀
首先,我有下表:
table<-data.frame(brand=c('duna','cars','cars','sea','sea','sea','mega','moon','moon'),model=c('mm','mm','mm','ll','ll','ll','tr','tr','tr'),version=c("2.8 sr cab. dupla 4x4 tdi","2.0 lsdakar 16v 4x4 hi-flex 5-p","2.4 ls cab. simples 4x2 flex 2-p","2.3 xl cab. simples 4x2 2-p","1.8 sx 5-p","1.0 mpfi joy 8v","hatch ls 1.0 8v","2.3 xlt cab. dupla 4x2 limited 4-p","1.4 fire ce xlt flex 2-p"))
brand model version
1 duna mm 2.8 sr cab. dupla 4x4 tdi
2 cars mm 2.0 lsdakar 16v 4x4 hi-flex 5-p
3 cars mm 2.4 ls cab. simples 4x2 flex 2-p
4 sea ll 2.3 xl cab. simples 4x2 2-p
5 sea ll 1.8 sx 5-p
6 sea ll 1.0 mpfi joy 8v
7 mega tr hatch ls 1.0 8v
8 moon tr 2.3 xlt cab. dupla 4x2 limited 4-p
9 moon tr 1.4 fire ce xlt flex 2-p
我不得不與下一個與之相匹配的:
table_match<-data.frame(brand=c('duna','cars','sea','mega','moon'),model=c('mm','mm','ll','tr','tr'),version=c('tdi','ls','xl','ls','xlt'))
table_match$id<-paste0(table_match$brand,table_match$model,table_match$version)
brand model version id
1 duna mm tdi dunammtdi
2 cars mm ls carsmmls
3 sea ll xl seallxl
4 mega tr ls megatrls
5 moon tr xlt moontrxlt
所以,這裏的問題是匹配brand
,從table_match
到table
。
例如,在table
如果brand=duna
,model=mm
和version
包含確切的詞"tdi"
,那麼它是一個匹配!所以id
(位於table_match
)與該匹配,將位於version
旁邊。
brand model version match
1 duna mm 2.8 sr cab. dupla 4x4 tdi dunammtdi
2 cars mm 2.0 lsdakar 16v 4x4 hi-flex 5-p
3 cars mm 2.4 ls cab. simples 4x2 flex 2-p carsmmls
4 sea ll 2.3 xl cab. simples 4x2 2-p seallxl
5 sea ll 1.8 sx 5-p
6 sea ll 1.0 mpfi joy 8v
7 mega tr hatch ls 1.0 8v megatrls
8 moon tr 2.3 xlt cab. dupla 4x2 limited 4-p moontrxlt
9 moon tr 1.4 fire ce xlt flex 2-p moontrxlt
建議看看package'fuzzyjoin',它使用提供的函數進行匹配。 – epi99