Title: | Like Set Tools in 'Base' Package but Keeps Duplicate Elements |
---|---|
Description: | The 'base' tools union() intersect(), etc., follow the algebraic definition that each element of a set must be unique. Since it's often helpful to compare all elements of two vectors, this toolset treats every element as unique for counting purposes. For ease of use, all functions in vecsets have an argument 'multiple' which, when set to FALSE, reverts them to the base::sets (alias for all the items) tools functionality. |
Authors: | Carl Witthoft [aut, cre] |
Maintainer: | Carl Witthoft <[email protected]> |
License: | LGPL-3 |
Version: | 1.4 |
Built: | 2024-12-19 06:25:51 UTC |
Source: | CRAN |
base
tools such as 'intersect' which does not reduce to unique elementsThe base
set-related tools follow the algebraic definition that each element of a set must be unique. Since it's often helpful to compare all elements of two vectors, this toolset treats every element as unique for counting purposes.
For ease of use, all functions in vecsets
have an argument multiple
which, when set to FALSE
, reverts them to the base
set tools functionality.
Package: | vecsets |
Type: | Package |
Version: | 3.0 |
Date: | 2021-03-08 |
License: | GPL-3 |
Carl Witthoft, with some code taken from Sven Hohenstein via Stack Overflow
Maintainer: Carl Witthoft [email protected]
Unlike the base::intersect
function, if the vectors have repeated elements in common, the intersection returns as many of these elements as are in whichever vector has fewer of them.
vintersect(x, y, multiple = TRUE)
vintersect(x, y, multiple = TRUE)
x |
A vector or an object which can be coerced to a vector |
y |
A vector or an object which can be coerced to a vector |
multiple |
Should repeated "multiple" items be returned? Default is |
A vector of the elements in the intersection of the two vectors. If multiple=FALSE
is set, only unique values are returned. If the intersection is empty, an empty vector of same type is returned, mimicking base::intersect
.
Carl Witthoft, with some code taken from Sven Hohenstein via Stack Overflow
intersect
, the CRAN package sets
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vintersect(x,y) vintersect(x,y,multiple=FALSE) intersect(x,y) #same as previous line
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vintersect(x,y) vintersect(x,y,multiple=FALSE) intersect(x,y) #same as previous line
This function first uses combn
to generate combinations of the desired size, then calculates all permutations of all said combinations.
vperm(x, m, FUN = NULL, ...)
vperm(x, m, FUN = NULL, ...)
x |
vector source for combinations, or integer n for x <- seq_len(n) |
m |
number of elements to choose in making the combinations |
FUN |
function to be applied to each combination; default NULL means the identity, i.e., to return the combination (vector of length m) |
... |
Additional arguments, if any, required for the function |
NA values are considered as valid elements and will be processed just as they are in combn
The input arguments are passed directly to combn
but with one important exception. combn
's argument "simplify" is forced to "TRUE" inside this function so as to allow the permutations to be more easily generated. If the user includes simplify = FALSE
in the ...
input, it will be overwritten.
An array within which each row contains one of the permutations.
Carl Witthoft, with some code taken from Sven Hohenstein via Stack Overflow
intersect
, the CRAN package sets
, perms
x <- c(1:5,3,3,3,2,NA,NA) xp <- vperm(x,4) #large array
x <- c(1:5,3,3,3,2,NA,NA) xp <- vperm(x,4) #large array
Finds all elements in first argument which are not in the second argument. Unlike the base::setdiff
function, if the vectors have repeated elements in common, only the "excess" number of a given element are returned.
vsetdiff(x, y, multiple = TRUE)
vsetdiff(x, y, multiple = TRUE)
x |
A vector or an object which can be coerced to a vector |
y |
A vector or an object which can be coerced to a vector |
multiple |
Should repeated "multiple" items be returned? Default is |
A vector of all elements in x
which are not in y
. If multiple=FALSE
is set, only unique values are returned.
Carl Witthoft
setdiff
, the CRAN package sets
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vsetdiff(x,y) vsetdiff(x,y,multiple=FALSE) setdiff(x,y) # same as previous line vsetdiff(y,x) #note the asymmetry
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vsetdiff(x,y) vsetdiff(x,y,multiple=FALSE) setdiff(x,y) # same as previous line vsetdiff(y,x) #note the asymmetry
Unlike the base::setequal
function, if the vectors have repeated elements in common, the count of these elements is checked. As a result, vectors of different lengths will never be "equal."
vsetequal(x, y, multiple = TRUE)
vsetequal(x, y, multiple = TRUE)
k
x |
A vector or an object which can be coerced to a vector |
y |
A vector or an object which can be coerced to a vector |
multiple |
Should repeated "multiple" items be returned? Default is |
A logical value indicating equality or inequality. If multiple=FALSE
is set, both input vectors are reduced to unique values before checking for equality.
Carl Witthoft
setequal
, the CRAN package sets
x <- c(1:5,3,3,3,2,NA,NA) y<- c(1:5,4,3,NA) vsetequal(x,y) vsetequal(x,y,multiple=FALSE) setequal(x,y) #same as previous line
x <- c(1:5,3,3,3,2,NA,NA) y<- c(1:5,4,3,NA) vsetequal(x,y) vsetequal(x,y,multiple=FALSE) setequal(x,y) #same as previous line
The base::union
function removes duplicates per algebraic set theory. vunion
does not, and so returns as many duplicate elements as are in either input vector (not the sum of their inputs.) In short, vunion
is the same as vintersect(x,y) + vsetdiff(x,y) + vsetdiff(y,x)
.
vunion(x, y, multiple = TRUE)
vunion(x, y, multiple = TRUE)
x |
A vector or an object which can be coerced to a vector |
y |
A vector or an object which can be coerced to a vector |
multiple |
Should repeated "multiple" items be returned? Default is |
A vector of the union of the two input vectors. If multiple
is set to FALSE
then the value returned is the same as base::union
.
Carl Witthoft
union
, the CRAN package sets
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vunion(x,y) vunion(x,y,multiple=FALSE) union(x,y) #same as previous line
x <- c(1:5,3,3,3,2,NA,NA) y<- c(2:5,4,3,NA) vunion(x,y) vunion(x,y,multiple=FALSE) union(x,y) #same as previous line