r-基于查找表有效地替换多列中的字符串



我有一个包含255行的查找表-值|替换。这是一个html代码表(例如,与空格字符"相对应(。表名:查找

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

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

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>, …

创建于2022-02-16由reprex包(v2.0.1(

我相信这是最快的。

最新更新