Grepl并在R中提取匹配

时间:2018-03-05 12:14:58

标签: r regex dataframe grepl

在R中我有:

library(tidyverse)
full_names <- tibble(FIRM = c("APPLE INC.", "MICROSOFT CORPORATION", "GOOGLE", "TESLA INC.", "ABBOTT LABORATORIES"), 
                 TICKER = c("AAPL", "MSFT", "GOOGL", "TSLA", "ABT"),
                 ID = c(111, 222, 333, 444, 555)) # a dataset with full names of firms, including some IDs
abbr_names <- c("Abbott", "Apple", "Coca-Cola", "Pepsi, "Microsoft", "Tesla") # a vector with abbreviated names of firms

我想检查缩写名称是否在全名数据集中,如果为true,则随后将full_names行与abbr_names向量匹配,如:

    [1]        [2]                    [3]   [4]
[1] Abbott     ABBOTT LABORATORIES    ABT   555
[2] Apple      APPLE INC.             AAPL  111
[3] Microsoft  MICROSOFT CORPORATION  MSFT  222
[4] Tesla      TESLA INC.             TSLA  444

尝试了几个str_extract和grepl函数,但还没能使它工作。

4 个答案:

答案 0 :(得分:3)

matches <- unlist(sapply(toupper(abbr_names), grep, x = full_names$FIRM, value = TRUE))

这将为您提供一个名称为缩写,公司为值

的向量
names(matches)
# [1] "ABBOTT"    "APPLE"     "MICROSOFT" "TESLA"  
c(firm_matches, use.names = FALSE)
# [1] "ABBOTT LABORATORIES"   "APPLE INC."            "MICROSOFT CORPORATION" "TESLA INC."  

有很多方法可以将它们组合在一起... ... ...

根据@Oscar的评论,我们得到了所需的输出,总共有两行代码:

matches <- unlist(sapply(toupper(abbr_names), grep, x = full_names$FIRM, value = TRUE))
tibble(ABBR_FIRM = names(matches), FIRM = matches) %>% left_join(., full_names, by = "FIRM")

答案 1 :(得分:1)

怎么样?

#

答案 2 :(得分:0)

另一种选择可能是例如......

map_int(abbr_names, ~ {
  idx <- grep(., full_names$FIRM, ignore.case = TRUE)
  if (length(idx) == 0) return(NA) else return(idx)
}) %>% 
  cbind(ABBR = abbr_names, FIRM = full_names$FIRM[.]) %>% 
  as.tibble() %>% 
  left_join(full_names, by = "FIRM") %>%
  complete(FIRM)

# A tibble: 4 x 5
  FIRM                  .     ABBR      TICKER    ID
  <chr>                 <chr> <chr>     <chr>  <dbl>
1 ABBOTT LABORATORIES   5     Abbott    ABT      555
2 APPLE INC.            1     Apple     AAPL     111
3 MICROSOFT CORPORATION 2     Microsoft MSFT     222
4 TESLA INC.            4     Tesla     TSLA     444

只是想发布它:)

答案 3 :(得分:0)

我建议,将所有单词打开为大写或小写。 grepl进行比较更容易实现功能。

我的代码:

library(tidyverse)

full_names <- tibble(FIRM = c("APPLE INC.", "MICROSOFT CORPORATION", "GOOGLE", "TESLA INC.", "ABBOTT LABORATORIES"), 
                     TICKER = c("AAPL", "MSFT", "GOOGL", "TSLA", "ABT"),
                     ID = c(111, 222, 333, 444, 555)) # a dataset with full names of firms, including some IDs

abbr_names <- c("Abbott", "Apple", "Coca-Cola", "Microsoft", "Tesla") # a vector with abbreviated names of firms

在这里,我创建了一个新列,我们想要索引grepl

的返回值
full_names$new_column <- NA

然后,我在名称中做了一个我们要在数据框中索引的循环

for(i in 1:length(abbr_names)){
  search_test <- grepl(tolower(substr(abbr_names[i], 0,4)), tolower(full_names$FIRM))
  position <- grep("TRUE", search_test)
  full_names$new_column[position] <- abbr_names[i]
}

结果是以下数据框:

   FIRM               TICKER    ID  new_column
1 APPLE INC.            AAPL     111 Apple     
2 MICROSOFT CORPORATION MSFT     222 Microsoft 
3 GOOGLE                GOOGL    333 NA        
4 TESLA INC.            TSLA     444 Tesla     
5 ABBOTT LABORATORIES   ABT      555 Abbott

“GOOG”不在abbr_names向量中,因此返回值为NA