i具有以下字母的向量:
my_alphs <- c("X","Y","Z")
给出带有星号(*)的字符串作为通配符:
my_str <- "LA**"
请注意,字符串的长度可以超过4,并且星号的位置可以在不同长度的任何地方。
我想根据my_alphs
中存储的字母枚举所有星号(*),从而导致此(我手动执行此操作):
LAXX
LAXY
LAXZ
LAYX
LAYY
LAYZ
LAZX
LAZY
LAZZ
我如何用R实现这一目标?
按需更改 my_str
和 my_alphs
。
> library(dplyr)
> library(stringr)
> library(purrr)
> do.call(expand.grid, list(rep(list(my_alphs), str_count(my_str, "\*")), stringsAsFactors=FALSE)) %>%
+ accumulate(function (str, replacement) str_replace(str, "\*", replacement), .init=my_str) %>%
+ last()
[1] "LAXX" "LAYX" "LAZX" "LAXY" "LAYY" "LAZY" "LAXZ" "LAYZ" "LAZZ"
这是一个基本R
解决方案,可以将*
replace_wildcards <- function(str, alphs) {
strs <- strsplit(str, "")[[1]]
combs <- do.call(expand.grid, list(alphs)[rep(1, sum(strs == "*"))])
frame <- do.call(cbind, lapply(strs, rep, NROW(combs)))
frame[, strs == "*"] <- as.matrix(combs)
apply(frame, 1, paste, collapse = "")
}
示例:
replace_wildcards("LA**", c("X","Y","Z"))
# [1] "LAXX" "LAYX" "LAZX" "LAXY" "LAYY" "LAZY" "LAXZ" "LAYZ" "LAZZ"
replace_wildcards("*N*Y*", c("1", "2"))
# "1N1Y1" "2N1Y1" "1N2Y1" "2N2Y1" "1N1Y2" "2N1Y2" "1N2Y2" "2N2Y2"
replace_wildcards("**_is_here", c("Q", "I", "R"))
# [1] "QQ_is_here" "IQ_is_here" "RQ_is_here" "QI_is_here" "II_is_here" "RI_is_here" "QR_is_here" "IR_is_here" "RR_is_here"