I am trying to select rows from a data frame based on the content of one of the columns. I am using grep() but when trying to match for the end of the content it only matches the last pattern that was submitted.
This is the code:
df1 <- data.frame(cName=c(
'A Co', 'B Co', 'C Co', 'D Co',
'F Llc', 'G Llc', 'H Llc', 'I Llc',
'P Inc', 'Q Inc', 'R Inc', 'S Inc'))
tName <- grep( ("Inc$ | Llc$"),df1$cName, value = T)
tName
[1] "F Llc" "G Llc" "H Llc" "I Llc"
I am expecting it to return all the occurrences of 'Inc' and 'Llc'. However, only the last one in the regular expression is returned. I have tried various combinations with brackets, parenthesis and [:space:] without success. What is wrong?
Thanks for any advice.
grep("Inc$|Llc$",df1$cName, value = TRUE). Or in case space is important, dogrep(" Inc$| Llc$",df1$cName, value = TRUE)