Description
Performs set union, intersection, (asymmetric!) difference, equality and membership on two vectors.Usage
union(x, y) intersect(x, y) setdiff(x, y) setequal(x, y) is.element(el, set)Arguments
- x, y, el, set
- vectors (of the same mode) containing a sequence of items (conceptually) with no duplicated values.
Details
Each ofunion,intersect,setdiffandsetequalwill discard any duplicated values in the arguments, and they applyas.vectorto their arguments (and so in particular coerce factors to character vectors).is.element(x, y)is identical tox %in% y.Values
Examples
(x <- c(sort(sample(1:20, 9)), NA)) (y <- c(sort(sample(3:23, 7)), NA)) union(x, y) intersect(x, y) setdiff(x, y) setdiff(y, x) setequal(x, y) ## True for all possible x & y : setequal( union(x, y), c(setdiff(x, y), intersect(x, y), setdiff(y, x))) is.element(x, y) # length 10 is.element(y, x) # length 8is.element()makes for more readable code, and is identical to%in%v <- c('a','b','c','e') is.element('b', v) 'b' %in% v ## both return TRUE is.element('f', v) 'f' %in% v ## both return FALSE subv <- c('a', 'f') subv %in% v ## returns a vector TRUE FALSE is.element(subv, v) ## returns a vector TRUE FALSE
Monday, October 13, 2014
sets {base}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment