3 回答

TA貢獻1993條經驗 獲得超6個贊
|
grep
or
"001|100|000"
grep
x <- c("1100", "0010", "1001", "1111")pattern <- "001|100|000"grep(pattern, x)[1] 1 2 3
grepl
:
grepl(pattern, x)[1] TRUE TRUE TRUE FALSE
?regex
編輯:paste
:
myValues <- c("001", "100", "000")pattern <- paste(myValues, collapse = "|")

TA貢獻1893條經驗 獲得超10個贊
stringr
require(stringr)mylist = c("1100", "0010", "1001", "1111")str_locate(mylist, "000|001|100")

TA貢獻1816條經驗 獲得超4個贊
對不起,這是一個附加的回答,但這是太多的評論行。
我只想提醒你,可以通過paste(..., collapse = "|")作為一個單一的匹配模式使用是有限的-見下文。也許有人能知道極限在哪里?誠然,這一數字可能不現實,但根據要執行的任務,不應將其完全排除在我們的考慮之外。
對于非常多的項,需要一個循環來檢查模式的每個項。
set.seed(0)
samplefun <- function(n, x, collapse){
paste(sample(x, n, replace=TRUE), collapse=collapse)
}
words <- sapply(rpois(10000000, 8) + 1, samplefun, letters, '')
text <- sapply(rpois(1000, 5) + 1, samplefun, words, ' ')
#since execution takes a while, I have commented out the following lines
#result <- grepl(paste(words, collapse = "|"), text)
# Error in grepl(pattern, text) :
# invalid regular expression
# 'wljtpgjqtnw|twiv|jphmer|mcemahvlsjxr|grehqfgldkgfu|
# ...
#result <- stringi::stri_detect_regex(text, paste(words, collapse = "|"))
# Error in stringi::stri_detect_regex(text, paste(words, collapse = "|")) :
# Pattern exceeds limits on size or complexity. (U_REGEX_PATTERN_TOO_BIG)
- 3 回答
- 0 關注
- 516 瀏覽
添加回答
舉報