From the course: R for Data Science: Lunch Break Lessons

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Simple string matching

Simple string matching

- [Instructor] There will be times when you want to do some simple string matching. For example, does this start with a string or does it end with a string? Or is that string anywhere nearby? You don't need to do complex regular expression matching. R provides a couple of operators that will help you with this need. Let's take a look at a few of them. First, I've created two vectors: one called haystack and one called needle. In haystack, the vector that we'll be searching through, I've put red, blue, green, another blue, and the word green forest, which you'll notice starts with the word green. The needle vector contains green, blue, cyan, and g. Let's use those two to find out which one is in the other. To do that, I can use match, M-A-T-C-H, and then I type in needle which is the vector I want to search for, followed by haystack, which is the vector I'm going to search in. When I hit return, I get the values three, two, NA, and NA. Let's examine what that actually means. The first…

Contents