Error in stri_detect_regex in R

2019-02-26 14:51发布

I am receiving this error

Error in stri_detect_regex(string, pattern, opts_regex = opts(pattern)) : Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)

when I run the code

  # find occurrences of initial dataframe
  named_RN$search <- map_int(named_RN$V1, function(x){sum(str_detect(final_RN$named_RN, pattern = x))})

in which named_RN$V1 looks like

aldosterone
renin
potassium
calcitrol

and final_RN$named_RN looks like

aldosterone, creatinine 
human, warfarin
aspirin, renin, calcitrol
magnesium, calcitrol

and my code aims to create a new variable within named_RN that shows the raw counts of each phrase, so that named_RN looks like

V1              search
aldosterone     1
renin           0
potassium       0
calcitrol       2

Please advise. Thanks.

1条回答
劫难
2楼-- · 2019-02-26 15:30

Since you are using fixed strings, not regular expressions, you need to tell the regex engine to use the patterns as plain, literal text. You can use it like this:

str_detect(final_RN$named_RN, fixed(x))
                              ^^^^^^^^

See "Fixed matches":

fixed(x) only matches the exact sequence of bytes specified by x. This is a very limited “pattern”, but the restriction can make matching much faster.

You might also consider coll(x) in case you want to use human-language collation rules while performing a case insensitive search.

查看更多
登录 后发表回答