Error in stri_detect_regex in R

2019-02-26 14:35发布

问题:

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:

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.