I have a column with text similar to the following:
cadenas <- c("el metro", "dos metrobuses", "pasó en el metro norte", "metro 12", "no")
I'm interested in only getting the elements that strictly have the word "meter", that is, elements 1, 3 and 4.
grepl("metro", cadenas)
Returns TRUE for the first 4 and FALSE for the last. I have tried many combinations to get only the ones that interest me without any success.
cadenas[grepl("metro [[:alnum:]]", cadenas)] #Regresa los elementos 3 y 4 (resultado más cercano al deseado)
cadenas[grepl("\\ metro [[:alnum:]]", cadenas)] #Regresa el elemento 3
cadenas[grepl("\\ metro", cadenas)] #Regresa los elementos 1,2 y 3
I have read many places without finding an answer that satisfies my requirements so any guidance would be greatly appreciated.
You can use the pattern
\\b
( word boundary / word limit ) to detectmetro
as a whole word: