r - 使用(如果可能)函数方法基于正则表达式 (regex) 的向量修改向量
问题描述
我有一个数据框,其中包含一些我想要修改的列,具体取决于它们是否将向量中包含的某些模式与正则表达式匹配
library(fuzzyjoin)
library(tidyverse)
(df <- tribble(~a,
"GUA-ABC",
"REF-CDE",
"ACC.S93",
"ACC.ATN"))
#> # A tibble: 4 x 1
#> a
#> <chr>
#> 1 GUA-ABC
#> 2 REF-CDE
#> 3 ACC.S93
#> 4 ACC.ATN
根据我要粘贴文本的模式,例如,对于那些在由点连接的链末尾包含 GUA- 粘贴“GUA001”的那些以及以相同方式包含 REF-粘贴“GUA002”的那些,能够获得以下内容:
# This is the resulting data.frame I need
#> # A tibble: 4 x 1
#> a
#> <chr>
#> 1 GUA-ABC.GUA001
#> 2 REF-CDE.GUA002
#> 3 ACC.S93
#> 4 ACC.ATN
我想了一些办法。
方法#1
# list of patterns to search
patterns <- c("\\b^GUA\\b", "\\b^REF\\b")
# Create a named list for recoding
model_key <- list("\\b^GUA\\b" = "GUA001",
"\\b^REF\\b" = "GUA002")
# Create a data.frame of regexs
(k <- tibble(regex = patterns))
#> # A tibble: 2 x 1
#> regex
#> <chr>
#> 1 "\\b^GUA\\b"
#> 2 "\\b^REF\\b"
# perform a regex_left_join to identify the pattern
df %>%
regex_left_join(k, by = c(a = "regex")) %>%
mutate(
across(regex, recode, !!!model_key),
a = case_when(
!is.na(regex) ~ str_c(a, regex, sep = "."),
TRUE ~ a)
) %>% select(-regex)
#> # A tibble: 4 x 1
#> a
#> <chr>
#> 1 GUA-ABC.GUA001
#> 2 REF-CDE.GUA002
#> 3 ACC.S93
#> 4 ACC.ATN
为什么这种方法不是最优的?原始数据框有数百万行,并且fuzzyjoin::regex_left_join
需要很长时间才能做到这一点。
方法#2
patron <- c("GUA001" = "\\b^GUA\\b", "GUA002" = "\\b^REF\\b")
newtex <- c("GUA001", "GUA002")
pegar <- function(string, pattern, text_to_paste) {
if_else(condition = str_detect(string, pattern),
true = str_c(string, text_to_paste, sep = "."),
false = string)
}
map2_dfr(.x = patron, .y = newtex, ~ pegar(string = df$a,
pattern = .x,
text_to_paste = .y))
#> # A tibble: 4 x 2
#> GUA001 GUA002
#> <chr> <chr>
#> 1 GUA-ABC.GUA001 GUA-ABC
#> 2 REF-CDE REF-CDE.GUA002
#> 3 ACC.S93 ACC.S93
#> 4 ACC.ATN ACC.ATN
由reprex 包于 2021-05-20 创建 (v2.0.0 )
使用方法#2我无法获得单列。
作为旁注,使用str_replace_all
和使用命名向量来替换字符串中的某些值目前似乎不是一个好的选择。
有没有办法更优化地做到这一点?
解决方案
一种使用stringr
and的选项purrr
可能是:
imap_dfr(model_key,
~ df %>%
filter(str_detect(a, .y)) %>%
mutate(a = str_c(a, .x, sep = "."))) %>%
bind_rows(df %>%
filter(str_detect(a, str_c(names(model_key), collapse = "|"), negate = TRUE)))
a
<chr>
1 GUA-ABC.GUA001
2 REF-CDE.GUA002
3 ACC.S93
4 ACC.ATN
推荐阅读
- regex - 在正则表达式中包含“顺序”,但使用 python 在正则表达式中排除“顺序”
- python - 无法从 PyPi 包外部读取 TXT 文件
- r - `testthat::expect_silent()` 似乎没有注意到 ggplot2 错误
- progressive-web-apps - 如何为 Web-push 选择合适的 TTL 值?
- c# - 调用外部库方法时任务超时后未结束
- r - 使用正则表达式区分 \q{} 和 \cite{} 的困难
- python - PyDub 从变量播放音频
- python - Python 中的预测值
- powershell - 未调用时设置执行策略错误
- dart - 从自定义输入小部件中获取值