**R – Aaron Schlegel**, and kindly contributed to R-bloggers)

The union and intersection set operations were introduced in a previous post using two sets, and . These set operations can be generalized to accept any number of sets.

###### Arbitrary Set Unions Operation

Consider a set of infinitely many sets:

It would be very tedious and unnecessary to repeat the union statement repeatedly for any non-trivial amount of sets, for example, the first few unions would be written as:

Thus a more general operation for performing unions is needed. This operation is denoted by the symbol. For example, the set above and the desired unions of the member sets can be generalized to the following using the new notation:

We can then state the following definition: For a set , the union of is defined by:

For example, consider the three sets:

The union of the three sets is written as:

Recalling our union axiom from a previous post, the union axiom states for two sets and , there is a set whose members consist entirely of those belonging to sets or , or both. More formally, the union axiom is stated as:

As we are now dealing with an arbitrary amount of sets, we need an updated version of the union axiom to account for the change.

Restating the union axiom:

For any set , there exists a set whose members are the same elements of the elements of . Stated more formally:

The definition of can be stated as:

For example, we can demonstrate the updated axiom with the union of four sets :

We can implement the set operation for an arbitrary amount of sets by expanding upon the function we wrote previously.

set.unions <- function(a, b, ...) { u <- a for (i in 1:length(b)) { if (!(b[i] %in% u)) { u <- append(u, b[i]) } } s <- list(...) for (i in s) { for (j in i) { if (!(j %in% u)) { u <- append(u, j) } } } return(u) }

Perform the set union operation of four sets:

a <- c(1,2,3) b <- c(3,4,5) c <- c(1,4,6) d <- c(2,5,7) set.unions(a, b, c, d) ## [1] 1 2 3 4 5 6 7

###### Intersections of an Arbitrary Number of Sets

The intersection set operation can also be generalized to any number of sets. Consider the previous set containing an infinite number of sets.

As before, writing out all the intersections would be tedious and not elegant. The intersection can instead be written as:

As before in our previous example of set intersections, there is no need for a separate axiom for intersections, unlike unions. Instead, we can state the following theorem, for a nonempty set , a set exists that such for any element :

Consider the following four sets:

The intersection of the sets is written as:

We can write another function to implement the set intersection operation given any number of sets.

set.intersections <- function(a, b, ...) { intersect <- vector() for (i in a) { if (i %in% b) { intersect <- append(intersect, i) } } s <- list(...) for (i in s) { for (j in i) { if (j %in% intersect) { intersect <- append(intersect, j) } } } intersect <- unique(intersect) return(intersect) }

Perform set intersections of the four sets specified earlier.

a <- c(1,2,3,5) b <- c(1,3,5) c <- c(1,4,5,3) d <- c(2,5,1,3) set.intersections(a, b, c, d) ## [1] 1 3 5

###### References

Enderton, H. (1977). Elements of set theory (1st ed.). New York: Academic Press.

The post Set Theory Arbitrary Union and Intersection Operations with R appeared first on Aaron Schlegel.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Aaron Schlegel**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...