【问题标题】:Using dplyr::mutate to loop through all available methods in stringdist使用 dplyr::mutate 循环遍历 stringdist 中的所有可用方法
【发布时间】:2018-02-01 16:57:57
【问题描述】:

我正在做一些模糊文本匹配来匹配学校名称。这是我的数据示例,它是 tibble 中的两列:

data <- tibble(school1 = c("abilene christian", "abilene christian", "abilene christian", "abilene christian"),
               school2 = c("a t still university of health sciences", "abilene christian university", "abraham baldwin agricultural college", "academy for five element acupuncture"))
data
# A tibble: 4 x 2
school1           school2                                
  <chr>             <chr>                                  
1 abilene christian a t still university of health sciences
2 abilene christian abilene christian university           
3 abilene christian abraham baldwin agricultural college   
4 abilene christian academy for five element acupuncture 

我想做的是使用stringdist 遍历所有可用的methods 并返回一个看起来像这样的表,其中除了每个方法的列和返回的值之外,我的原始文本仍然存在:

# A tibble: 4 x 12
  school1           school2       osa    lv    dl hamming   lcs qgram cosine jaccard    jw soundex
  <chr>             <chr>       <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>  <dbl>   <dbl> <dbl>   <dbl>
1 abilene christian a t still …  29.0  29.0  29.0     Inf  36.0  24.0 0.189    0.353 0.442    1.00
2 abilene christian abilene ch…  11.0  11.0  11.0     Inf  11.0  11.0 0.0456   0.200 0.131    0   
3 abilene christian abraham ba…  28.0  28.0  28.0     Inf  35.0  25.0 0.274    0.389 0.431    1.00
4 abilene christian academy fo…  28.0  28.0  28.0     Inf  37.0  29.0 0.333    0.550 0.445    1.00

我可以使用以下方法使用 for 循环使其工作:

  method_list <- c("osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw", "soundex")
    for (i in method_list) {
  data[, i] <- stringdist(data$school1, data$school2, method = i)
}

我想做的是将它转换为更易读的 dplyr 语法,但我无法让循​​环与 mutate 一起工作。这是我所拥有的:

for (i in method_list) {
      ft_result <- data %>% 
                     mutate(i = stringdist(school1, school2, method = i))            
    }

运行此操作会返回 1 个附加列添加到我的原始数据中,称为“i”,每行的值为 1。

问题 1:for 循环是完成我想要达到的目标的最佳方式吗?我查看了 purrr 以查看是否可以使用 map 或 invoke 之类的东西,但我认为这些函数中的任何一个都不能满足我的要求。

问题 2:如果要使用 for 循环,我怎样才能使其与 mutate 一起使用?我尝试使用 mutate_at,但也没有用。

【问题讨论】:

    标签: r for-loop dplyr purrr stringdist


    【解决方案1】:

    这似乎是一个使用purrr::map_dfc的好地方

    这里的一般想法是通过将每个方法作为输入传递的函数进行映射,并将结果包装在数据帧中。 purrr::set_names 也派上用场了。


    library(tidyverse)
    library(stringdist)
    
    method_list <- c("osa", "lv", "dl", "hamming", "lcs", "qgram",
                     "cosine", "jaccard", "jw", "soundex")
    
    tb <- starwars[c("name", "homeworld")]
    
    method_list %>%
      map_dfc(function(str_method) {
        data_frame(stringdist(tb$name, tb$homeworld, method = str_method))
        }
      ) %>%
      set_names(method_list) %>%
      bind_cols(tb, .)
    #> Warning in do_dist(a = b, b = a, method = method, weight = weight, maxDist
    #> = maxDist, : Non-printable ascii or non-ascii characters in soundex.
    #> Results may be unreliable. See ?printable_ascii.
    #> # A tibble: 87 x 12
    #>                  name homeworld   osa    lv    dl hamming   lcs qgram
    #>                 <chr>     <chr> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
    #>  1     Luke Skywalker  Tatooine    13    13    13     Inf    18    18
    #>  2              C-3PO  Tatooine     8     8     8     Inf    13    13
    #>  3              R2-D2     Naboo     5     5     5       5    10    10
    #>  4        Darth Vader  Tatooine     8     8     8     Inf    13    13
    #>  5        Leia Organa  Alderaan     8     8     8     Inf    11     9
    #>  6          Owen Lars  Tatooine     9     9     9     Inf    15    11
    #>  7 Beru Whitesun lars  Tatooine    16    16    16     Inf    22    16
    #>  8              R5-D4  Tatooine     8     8     8     Inf    13    13
    #>  9  Biggs Darklighter  Tatooine    14    14    14     Inf    19    17
    #> 10     Obi-Wan Kenobi   Stewjon    13    13    13     Inf    17    15
    #> # ... with 77 more rows, and 4 more variables: cosine <dbl>,
    #> #   jaccard <dbl>, jw <dbl>, soundex <dbl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 2021-07-27
      • 2016-10-15
      • 2020-08-20
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多