Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using grep to identify unique string

Tags:

r

Let's say I have a vector string:

words <- c("Guardian","ia","librarian")

If I grep for "ia", it would return all three.

grep("ia",words) # 1 2 3 

How can I make it so that it ONLY finds the 2nd term, the one with nothing else in it?

Note: I can do the opposite, with something like grep(".+ia|ia+.",words) but I'm not sure how to only return the 2nd position in this case.

like image 673
Brandon Bertelsen Avatar asked Feb 18 '23 08:02

Brandon Bertelsen


1 Answers

Use regular expressions to limit grep to just the letters you want:

grep("^ia$", words) # 2
like image 138
Erik Shilts Avatar answered Feb 27 '23 12:02

Erik Shilts