I am using the following code to extract matching sentences from a column in . adataframe:
Data$Prep <- grep("the preparation was", unlist(strsplit(Data$REPORT_TEXT, '(?<=\\.)\\s+', perl=TRUE)), value=TRUE, ignore.case = TRUE)
The problem is that not all the rows in the data frame column contain that matching pattern, so the returned vector is shorter than the data frame itself resulting in an error
*tmp*, Prep, value = c(4L, 22L, 41L, 67L, :
replacement has 685 rows, data has 700
Is there a way to avoid this ? is there a way to return empty string or NA when the searched string doesnt contain the matching words?