| Start |
|---|
| Cartesian or "cross" products of vectors To find every vector of the form (x, y) where x is drawn from vector X and y from Y, we use The result is a data.frame with one column for each vector passed to it. Often, we want to take the Cartesian product of sets rather than to expand a "grid" of vectors. We can use Applying functions to combinationsIf you then want to apply a function to each resulting combination This approach works for as many vectors as we need, but in the special case of two, it is sometimes a better fit to have the result in a matrix, which can be achieved with For related concepts and tools, see the combinatorics topic. |
Set operators for pairs of vectors Comparing setsIn R, a vector may contain duplicated elements: However, a set contains only one copy of each element. R treats a vector like a set by taking only its distinct elements, so the two vectors above are regarded as the same: Combining setsThe key functions have natural names: These are all documented on the same page, |
| Set membership for vectors The Each element on the left is treated individually and tested for membership in the set associated with the vector on the right (consisting of all its distinct elements). Unlike equality tests, The documentation is at |
| Make unique / drop duplicates / select distinct elements from a vector
Values are returned in the order they first appeared.
|
| Measuring set overlaps / Venn diagrams for vectors To count how many elements of two sets overlap, one could write a custom function: A Venn diagram, offered by various packages, can be used to visualize overlap counts across multiple sets. RemarksA set contains only one copy of each distinct element. Unlike some other programming languages, base R does not have a dedicated data type for sets. Instead, R treats a vector like a set by taking only its distinct elements. This applies to the set operators, For a true set data type in R, the Rcpp package provides some options (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference