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

Set operations (union, intersection, difference)

This lesson covers unions, intersections, and differences in R, with the aim of understanding the basics of set operations.

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, 4) and c(3, 4, 5, 6) and shows the results. 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)