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

Unlock this course with a free trial

Join today to access over 23,400 courses taught by industry experts.

combn finds combinations

combn finds combinations

- [Instructor] You will need to generate combinations of all elements in a vector, and R provides combine just for that purpose. Here's how to use it. First, I've created a vector called rawcats and it contains three letters, a, c, t, which is an alphabetized cat. And using that, I can use the repeat function, r-e-p, and then type in rawcats. And I'd like it to repeat three times, and I'll show you why I'm using this here in a second. But what you'll see is a-c-t-a-c-t-a-c-t. Now, I would like to find all of the combinations of three instances of rawcats. I'm going to put that into isCat and assign that c-o-m-b-n, which is the function we're looking at here. And I would like to combine all instances of three instances of cats, r-a-w-c-a-t-s and I want to repeat it three times, and then I want combine to select three elements. Now when I hit return, you'll see that I've got a new vector called isCat, it's an array one through three and one through 84. So if we look at it, isCat, you'll…

Contents