sweetmusicality sweetmusicality - 27 days ago 15
R Question

Error in stri_detect_regex in R

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
looks like


looks like

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

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

V1 search
aldosterone 1
renin 0
potassium 0
calcitrol 2

Please advise. Thanks.

Answer Source

Since you are using fixed strings, not regular expressions, you need to tell the regex engine to use the patterns as plain 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.