在R中,我如何用所有选项替换字符串中的模糊字符,返回一个可能的输出向量?

问题描述 投票:0回答:1

这是我想解决的问题。

#Original String
#Where ! is a ambiguous character which represent 2:n different characters. Here i assume it stands for either "1" or "2". 
I also have multiple ambiguity characters e.g. "?" = "3" or "4".

"A!!C!!D?"

#I want to know all forms a disambiguated string could take.

I.e, in this case .!!.!!.? could produce
4*4*2 = 32 possibilities. 

#I want a function to return all possibilities as a vector.

#e.g. desired return
c("A11C11D3", "A11C12D3", "A11C21D3", "A11C22D3",
  "A12C11D3", "A12C12D3", "A12C21D3", "A12C22D3",
...
)

是否有一个我遗漏的简单函数可以做到这一点,或者我需要用gsub或类似的函数从头开始建立它。

r string replace str-replace stringr
1个回答
2
投票

一种方法是将字符串拆分,用它们可能的值替换模糊字符,生成可能的组合,然后再粘贴到一起。

mystring <- "A!!C!!D?"

lapply(strsplit(mystring, ""), function(x) {
  res <- lapply(x, function(y)
    switch(y,
      "!" = 1:2,
      "?" = 3:4,
      y)
    )
  do.call(paste0, expand.grid(res))
})

[[1]]
 [1] "A11C11D3" "A21C11D3" "A12C11D3" "A22C11D3" "A11C21D3" "A21C21D3" "A12C21D3" "A22C21D3" "A11C12D3" "A21C12D3" "A12C12D3" "A22C12D3"
[13] "A11C22D3" "A21C22D3" "A12C22D3" "A22C22D3" "A11C11D4" "A21C11D4" "A12C11D4" "A22C11D4" "A11C21D4" "A21C21D4" "A12C21D4" "A22C21D4"
[25] "A11C12D4" "A21C12D4" "A12C12D4" "A22C12D4" "A11C22D4" "A21C22D4" "A12C22D4" "A22C22D4"
© www.soinside.com 2019 - 2024. All rights reserved.