R can perform different operations in sets, such as union, intersection, asymmetric difference of two sets, etc. Specifically, the following operations are available in R for set operations.
Operator
Usage
Definition
union
union(x, y)
Union of sets x and y
intersect
intersect(x, y)
Intersection of sets x and y
setdiff
setdiff(x, y)
Asymmetric difference between sets x andy (Elements in x but not in y)
Although x and x2 have different length, they have the same UNIQUE elements sosetequal(x, x2) returns a TRUE value.
is.element(x, y) is identical to x %in% y. The return value of is.element is a vector of TRUE and FALSE with the same length as x, which indicates whether each element of x is an element of y or not.
> is.element(x, y) # vector of length 10
[1] FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
> is.element(y, x) # vector of length 8
[1] FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE