根据查找有效地替换多列中的字符串 table

Efficiently replace string in multiple columns based on lookup table

我有一个包含 255 行的查找 table - 值 |替换。它是 table 用于 html 代码(例如   对应于 space 字符 ' ')。 Table 姓名:查找

我的 table 中有 6 列(共 50 列)有 HTML 个字符需要替换以提高易读性,它目前有 600 行,但可能会增加。 Table 姓名:exp

我想出的代码基于 dplyr 和一个 for 循环:它在查找中逐行查找 table 并检查目标变量中的匹配项。

len <- nrow(lookup)
for (i in 1:len){
  exp <- exp %>% 
         mutate_at(vars(c(var1, var2, var3, var4, var6, var8)), 
                   funs(gsub(pattern = lookup[i,1], replacement = lookup[i,2], x = .)))
}

运行需要相当长的时间,请问有没有更有效的运行替代方法?

添加数据示例以备将来参考

查找:

Pattern Replacement
&cent;  ¢
&amp;   &
&reg;   ®
&trade  ™
&copy;  ©
&current;   ¤
&gt;    >
&lt;    <
&nbsp;  
&euro;  €
&quot;  “
&apos;  ‘

Exp:

> example
# A tibble: 3 x 4
  `Example 1`                               `Example 2`                             `Example 3`                        `Example 4`                        
  <chr>                                     <chr>                                   <chr>                              <chr>                              
1 &cent; It denotes Cent Sign of currency   &tradeTrade Mark                        &gt;It denotes greater than sign   &euro;It defines the British Euro ~
2 &amp;It denotes frequently used Ampersan~ &copy;Gives Copy-right Symbol           &lt;It denotes lesser than sign    &quot;Gives double quotes in a giv~
3 &reg;Gives Registered Symbol              &current; It defines a Generic currenc~ &nbsp;It defines for Non-Breaking~ &apos;Includes Apostrophe in a sen~

使用 stringi 中的 stri_replace_all_fixed,您可以一次替换多个模式。语法有点混乱,但是当您设置 vectorise_all = FALSE 时,它会用相应的替换项替换所有模式的所有实例。

首先,让我们创建一些示例数据,因为您没有提供任何数据:

library(tidyverse)
set.seed(1)
exp <- data.frame(matrix(sample(LETTERS, 1000, replace = TRUE), ncol = 100))

lookup <- tribble(
  ~pattern, ~replacement,
  "A",     ":",
  "F",     " ",
  "Y",     "Test"
)

在这种情况下使用mutate + across这是mutate_at的新版本(mutate_at正在慢慢淘汰):

exp %>% 
  mutate(across(c(X1, X3), ~ stringi::stri_replace_all_fixed(
    str = .x,
    pattern = lookup[["pattern"]],
    replacement = lookup[["replacement"]],
    vectorise_all = FALSE
  ))) %>% 
  as_tibble()
#> # A tibble: 10 × 100
#>    X1    X2    X3    X4    X5    X6    X7    X8    X9    X10   X11   X12   X13  
#>    <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#>  1 Test  A     U     L     T     Y     N     H     M     V     W     B     U    
#>  2 D     U     E     O     T     W     B     F     H     L     S     J     L    
#>  3 G     U     I     A     Z     X     M     W     Y     P     V     A     G    
#>  4 :     J     Test  T     L     F     R     L     P     A     R     K     X    
#>  5 B     V     N     C     Y     Z     V     F     Y     M     Z     Z     U    
#>  6 W     N     E     F     W     G     N     H     W     U     P     O     V    
#>  7 K     J     E     J     F     S     F     G     N     F     K     Z     H    
#>  8 N     G     B     J     Y     J     A     K     T     Q     J     X     A    
#>  9 R     I     J     F     H     F     S     Q     G     I     G     J     S    
#> 10 S     O     Test  O     L     X     S     D     M     G     S     P     Z    
#> # … with 87 more variables: X14 <chr>, X15 <chr>, X16 <chr>, X17 <chr>,
#> #   X18 <chr>, X19 <chr>, X20 <chr>, X21 <chr>, X22 <chr>, X23 <chr>,
#> #   X24 <chr>, X25 <chr>, X26 <chr>, X27 <chr>, X28 <chr>, X29 <chr>,
#> #   X30 <chr>, X31 <chr>, X32 <chr>, X33 <chr>, X34 <chr>, X35 <chr>,
#> #   X36 <chr>, X37 <chr>, X38 <chr>, X39 <chr>, X40 <chr>, X41 <chr>,
#> #   X42 <chr>, X43 <chr>, X44 <chr>, X45 <chr>, X46 <chr>, X47 <chr>,
#> #   X48 <chr>, X49 <chr>, X50 <chr>, X51 <chr>, X52 <chr>, X53 <chr>, …

reprex package (v2.0.1)

创建于 2022-02-16

这是我相信的速度。