2017-05-24 109 views
2

我遇到以下與匹配不同數據幀有關的問題。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_matchtable

例如,在table如果brand=dunamodel=mmversion包含確切的詞"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 
+1

建議看看package'fuzzyjoin',它使用提供的函數進行匹配。 – epi99

回答

1

fuzzy_join兩個同等條件==,並從stringr第三str_detect。我不知道爲什麼模糊加入使得每三個副本,所以添加unique() 注:添加stringsAsFactors = FALSE測試數據

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") 
        stringsAsFactors = FALSE) 
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'), 
         stringsAsFactors = FALSE) 

library(fuzzyjoin) 
library(stringr) 

fuzzy_join(table, table_match, 
    by = c("brand", "model", "version"), 
    match_fun = c(`==`, `==`, function(x,y) { str_detect(x, paste0("\\b", y, "\\b"))})) %>% 
    unique() 

# brand.x model.x       version.x brand.y model.y version.y 
# 1  duna  mm   2.8 sr cab. dupla 4x4 tdi duna  mm  tdi 
# 4  cars  mm 2.4 ls cab. simples 4x2 flex 2-p cars  mm  ls 
# 7  sea  ll  2.3 xl cab. simples 4x2 2-p  sea  ll  xl 
# 10 mega  tr     hatch ls 1.0 8v mega  tr  ls 
# 13 moon  tr 2.3 xlt cab. dupla 4x2 limited 4-p moon  tr  xlt 
# 16 moon  tr   1.4 fire ce xlt flex 2-p moon  tr  xlt 
+0

您的代碼與字符串「lsdakar」匹配,如果您查看他的示例輸出,則該字符串不是原始海報所需的。 – jsb

+0

使用@Kristoferson的正則表達式更正並添加唯一() – epi99

+0

出色的貢獻!此外,使用包含邊界詞語的paste0,它與我想要的完全匹配。 – lolo

3

我想我們可以做一個合併,然後使用正則表達式進行過濾。給這個一杆

dat = merge(table, table_match, by = c("brand", "model")) 


dat$match = mapply(function(x, y) grepl(paste("\\b", x, "\\b", sep = ""), y), dat$version.y, dat$version.x) 

dat$match = ifelse(dat$match, dat$id, "") 
dat = dat[ , !colnames(dat) %in% c("version.y", "id")] 
colnames(dat)[3] = "version" 

dat = dat[with(dat, order(brand)), ] 

brand model       version  match 
1 cars mm 2.0 lsdakar 16v 4x4 hi-flex 5-p   
2 cars mm 2.4 ls cab. simples 4x2 flex 2-p carsmmls 
3 duna mm   2.8 sr cab. dupla 4x4 tdi dunammtdi 
4 mega tr     hatch ls 1.0 8v megatrls 
5 moon tr 2.3 xlt cab. dupla 4x2 limited 4-p moontrxlt 
6 moon tr   1.4 fire ce xlt flex 2-p moontrxlt 
7 sea ll  2.3 xl cab. simples 4x2 2-p seallxl 
8 sea ll      1.8 sx 5-p   
9 sea ll     1.0 mpfi joy 8v 
2

您也可以嘗試regex_join()fuzzyjoin包。注:我在table_match的字符串'ls'周圍增加了空格,以便正則表達式與table中的字符串'lsdakar'不匹配,因爲這不是原始發佈者想要的。

library(fuzzyjoin) 

# Use data_frame() to get rid of stringsAsFactors problem 
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" 
    ) 
) 

# Use data_frame() here too 
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') 
) 

# Use regex_semi_join to find the matches 
regex_semi_join(table, table_match, by = c(brand = "brand", 
    model = "model", version = "version")) 

# A tibble: 6 × 3 
    brand model       version 
    <chr> <chr>        <chr> 
1 duna mm   2.8 sr cab. dupla 4x4 tdi 
2 cars mm 2.4 ls cab. simples 4x2 flex 2-p 
3 sea ll  2.3 xl cab. simples 4x2 2-p 
4 mega tr     hatch ls 1.0 8v 
5 moon tr 2.3 xlt cab. dupla 4x2 limited 4-p 
6 moon tr   1.4 fire ce xlt flex 2-p 
> 

# Use regex_anti_join to find the non-matches 
regex_anti_join(table, table_match, by = c(brand = "brand", 
    model = "model", version = "version")) 

# A tibble: 3 × 3 
    brand model       version 
    <chr> <chr>       <chr> 
1 cars mm 2.0 lsdakar 16v 4x4 hi-flex 5-p 
2 sea ll      1.8 sx 5-p 
3 sea ll     1.0 mpfi joy 8v 
>