我们可以使用
stringr::str_extract(patient_condition, "P[0-9]+")
#[1] "P1" "P1" "P1" "P1" "P2" "P2" "P3"
Misc Replies
就我而言,这个答案是有效的.但我想我要问的问题是从给定一些匹配值的向量中提取子字符串.这意味着,如果我想提取字符(即前置、后置、浓缩等),这个答案将不起作用
to_match <- c("Pre", "Post", "Enriched")
在这种情况下,我们可以使用
## R-level loop through `to_match`
tmp <- t(sapply(to_match, stringr::str_extract, string = patient_condition))
tmp[!is.na(tmp)]
#[1] "Pre" "Post" "Enriched" "Post" "Pre" "Post" "Pre"
或
## convert multiple matches to REGEX "或" operation `|`
stringr::str_extract(patient_condition, paste0(to_match, collapse = "|"))
#[1] "Pre" "Post" "Enriched" "Post" "Pre" "Post" "Pre"
ThomasIsCoding's answer使用gregexpr
+regmatches
也是一个很好的 Select .
注意,这是进行exact个子字符串匹配.