R- Problem with identifying rows that contains a string of interest

Viewed 24

I have the following dataframe. here's a sample of my data :

> sample_n(d2, 10)
# A tibble: 10 × 2
   nct_id      description                                                                                                                                                 
   <chr>       <chr>                                                                                                                                                            
 1 NCT01573442 "The maximum grade for each type of toxicity will be recorded for each patient, and frequency tables will be reviewed to determine toxicity patterns within pati…
 2 NCT01887158 "Tolerability will be assessed by using a questionnaire completed by the patients on arrival at the endoscopy unit before colonoscopy.\r\nThe patient acceptance…
 3 NCT02663973  NA                                                                                                                                                              
 4 NCT02730546 "The distribution of TTR will be estimated using the method of Kaplan-Meier. Two-year relapse-free rate and confidence interval will be estimated based on Kapla…
 5 NCT02872701 "Number of patients where at least one positive margin is identified with only OTL-38 and NIR."                                                                  
 6 NCT03239184 "It will be evaluated by the Response Evaluation Criteria in Solid Tumors(RECIST)"                                                                             
 7 NCT02043730 "All patients must be considered in response analysis, including those who discontinue treatment or who die for any reason prior to response evaluations"        
 8 NCT03804307 "reduction of initial symptoms after radiotherapy, assessed with Likert scale (pain, bleeding, coughing, dysphagia, dyspnea, brachial plexopathy, lymphedema)"   
 9 NCT02472080 "overall response rate measured (complete and partial responses) by CT-scan according to RECIST criteria v1.1"                                                   
10 NCT02493114  NA  

What i want to do is to search for some words in the column 'description' of my dataframe. I have a list of words I'm interested in (more than 1000 words), here's a few of them.

list_of_words = c("TTR", "(Q)uality of  life", "RECIST","(Q)2-of-scale(4)") 

here's my code :

new_df = df %>% 
  mutate(found = str_extract_all(description, paste(list_of_words , collapse="|"))) %>% 
  unnest(found, keep_empty = TRUE)

my code works perfectly fine for words like 'TTR and RECIST' but it raises an error for '(Q)uality of life and (Q)2-of-scale(4)' because of the parenthesis ()

how can I fix this?

1 Answers

The error is because your list_of_wordsvector contains regex metacharacters, namely parentheses, which need to be escaped if used literally. Escaping, in R, is done with two backslashes. Rewrite the vector thus:

list_of_words = c("TTR", "\\(Q\\)uality of  life", "RECIST","\\(Q\\)2-of-scale\\(4\\)") 
Related