【发布时间】:2018-01-31 09:50:31
【问题描述】:
我收到此错误
stri_detect_regex(string, pattern, opts_regex = opts(pattern)) 中的错误: 正则表达式模式中不正确的嵌套括号。 (U_REGEX_MISMATCHED_PAREN)
当我运行代码时
# find occurrences of initial dataframe
named_RN$search <- map_int(named_RN$V1, function(x){sum(str_detect(final_RN$named_RN, pattern = x))})
named_RN$V1 的样子
aldosterone
renin
potassium
calcitrol
和final_RN$named_RN 看起来像
aldosterone, creatinine
human, warfarin
aspirin, renin, calcitrol
magnesium, calcitrol
我的代码旨在在 named_RN 中创建一个新变量,该变量显示每个短语的原始计数,因此 named_RN 看起来像
V1 search
aldosterone 1
renin 0
potassium 0
calcitrol 2
请指教。谢谢。
【问题讨论】:
-
您在正则表达式模式之一中的括号嵌套不正确。检查例如
stringr::str_detect(c("a","a("), c("a", "a("))与stringr::str_detect(c("a","a("), stringr::fixed(c("a", "a(")))。也许摆脱括号或使用固定匹配? -
如果你使用固定字符串而不是正则表达式匹配,你真的只需要使用
str_detect(final_RN$named_RN, fixed(x)) -
@Wiktor,成功了!介意将其发布为答案,以便我接受吗?
标签: r regex dataframe text error-handling