Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 27 / 68

Set operations (union, intersect, setdiff)

This lesson covers set operations with R's union, intersect, and setdiff, with the aim of calculating unions, intersections, and differences.

The union(), intersect(), and setdiff() functions calculate the union, intersection, and difference of sets directly.

The sample code applies each function to the two vectors c(1, 2, 3) and c(2, 3, 4) and shows the union, intersection, and difference. It is a clear design that maps the mathematics straight onto functions.

A common early stumble is that these functions also remove duplicates. Unlike a plain concatenation with c(), a set operation collapses repeated values into one.

In professional work, set operations come up in data analysis whenever you want the data common to two groups.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)