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.

Sets: Union, intersect, and difference

Sets: Union, intersect, and difference

- R has a complete collection of set operators. Let's take a look at union, intersect, and differences. First we need two vectors to compare. So I'll create Vector A and Vector B. Notice that both Vector A and Vector B have the number 10 as part of their contents. While first of all, I'd like to combine those two or form a union. Not surprisingly the command for this is union and I describe what I'd like to combine. So Vector A and Vector B and I hit return and you'll see that it returns a union of Vector A and Vector B. Notice that this is a unique union. So 10 is not duplicated. Now I'd like to find the intersection between Vector A and Vector B. Intersect. Vector A. And Vector B. And when I hit return 10 is the intersecting number between those two vectors. Now I may want to know what's unique about Vector A compared to Vector B? To do that I'll use setdiff and I type in Vector A compared to Vector B. And this'll tell me what's unique about Vector A. One, two, three, four, five…

Contents