2017-05-03 33 views
2

我正在通过按零售商名称对采购进行分组来分析我的银行对帐单,然后可以使用dplyr函数分析生成的数据框。我的方法使用自定义函数,但我很想知道是否有更高效的方法。例如,是否有任何包可以使用数据帧列之间的复杂匹配逻辑来连接数据框?R银行对帐单分组

debug(FindRetailer) 

FindRetailer<-function(Purchase){ 
    P <- toupper(Purchase) 
    for(z in 1:length(RetailerNames)){ 
    Retailer<-toupper(RetailerNames[z]) 
    HasFound=grepl(Retailer,P) 
    if(HasFound==TRUE){ 
     return(str_to_title(Retailer)) 
    } 
    } 
    return("Donno") 
} 

Statement <- data.frame(
    Purchase = c("abc Aldi xyz","a Kmart bcd","a STARBUCKS ghju","abcd MacD efg"), 
    Amount = c(235,23,789,45)) 

RetailerNames<- c("Aldi","Kmart","Starbucks","MacD") 

# what I need 
Result <- data.frame(
    Purchase = c("abc Aldi xyz","a KMART bcd","a STARBUCKS mmm","abcd MACD efg"), 
    Amount = c(235,23,789,45), 
    Retailer = c("Aldi","Kmart","Starbucks","Macd")) 

# this works using custom function 
NewStatment<-Statement %>% 
    rowwise() %>% 
    mutate(Retailer=FindRetailer(Purchase)) 

# is this possible: join dataframes using complex string matching? 
# this doesn't work yet 
TestMethod<-Statement %>% 
    left_join(RetailerNames,by="Statement.Purchase %in% RetailerNames") 

回答

4


library(tidyverse) 
library(glue) 
Statement <- data.frame(
    Purchase = c("abc Aldi xyz","a Kmart bcd","a STARBUCKS ghju","abcd MacD efg"), 
    Amount = c(235,23,789,45)) 

RetailerNames<- c("Aldi","Kmart","Starbucks","MacD") 


Statement %>% 
    mutate(
    Retailer = Purchase %>% 
     str_extract(RetailerNames %>% collapse(sep ="|") %>% regex(ignore_case = T)) 
    ) 
#>   Purchase Amount Retailer 
#> 1  abc Aldi xyz 235  Aldi 
#> 2  a Kmart bcd  23  Kmart 
#> 3 a STARBUCKS ghju 789 STARBUCKS 
#> 4 abcd MacD efg  45  MacD 

如果你想要去的left_join路线,尽量

library(fuzzyjoin) 

RetailerNames<- data_frame(Retailer = c("Aldi","Kmart","Starbucks","MacD")) 

Statement %>% 
    regex_left_join(RetailerNames, by = c(Purchase="Retailer")) 
+0

谢谢,我以为会有一个简单的解决方案。我会看看''fuzzyjoin''也 – Zeus

+0

我编辑的解决方案,因为我的原始只是因为幸运的巧合。我目前的解决方案涉及将零售商名称向量折叠为正则表达式字符串 – yeedle

+0

感谢您的纠正和模糊逻辑方法 – Zeus