c("DescTools", "sets")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
c(4, 5, 6)
a <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
b <- c(2, 1, 3, 2, 1)
m <- c(5, 3, 1, 3, 4, 4)
n <- c(1, 1, 2, 2)
x <- c(2, 1) y <-
setequal(x, y)
[1] TRUE
duplicated(c(1, 1, 1, 3, 3, 4, 4))
[1] FALSE TRUE TRUE FALSE TRUE FALSE TRUE
unique(c(1, 1, 1, 3, 3, 4, 4))
[1] 1 3 4
length(unique(c("A", "B", "C", "C", "B", "B", "A", "C", "C", "A")))
[1] 3
AllDuplicated()
from package DescTools
indicates all occurrences of a duplicated value, even the first one.
library(DescTools)
AllDuplicated(c(1, 1, 1, 3, 3, 4, 4))
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
union(m, n)
[1] 2 1 3 5 4
intersect(m, n)
[1] 1 3
setdiff(m, n)
[1] 2
setdiff(n, m)
[1] 5 4
union(setdiff(m, n), setdiff(n, m))
[1] 2 5 4
is.element(c(29, 23, 30, 17, 30, 10), c(30, 23))
[1] FALSE TRUE TRUE FALSE TRUE FALSE
c("A", "Z", "B") %in% c("A", "B", "C", "D", "E")
[1] TRUE FALSE TRUE
all(a %in% b)) (AinB <-
[1] TRUE
all(b %in% a)) (BinA <-
[1] FALSE
& !BinA AinB
[1] TRUE
sets
library(sets)
set(4, 5, 6)
sa <- set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sb <- set(2, 1, 3, 2, 1)
sm <- set(5, 3, 1, 3, 4, 4)
sn <- set(1, 1, 2, 2)
sx <- set(2, 1)
sy <- 4
se <-
set_is_empty(sa)
[1] FALSE
set_cardinality(sx)
[1] 2
set_power(sm)
{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
set_cartesian(sa, sx)
{(4, 1), (4, 2), (5, 1), (5, 2), (6, 1), (6, 2)}
set_is_equal(sx, sy)
[1] TRUE
set_union(sm, sn)
{1, 2, 3, 4, 5}
set_intersection(sm, sn)
{1, 3}
set_symdiff(sa, sb)
{1, 2, 3, 7, 8, 9, 10}
set_complement(sm, sn)
{4, 5}
set_is_subset(sa, sb)
[1] TRUE
set_is_proper_subset(sa, sb)
[1] TRUE
set_contains_element(sa, se)
[1] TRUE
try(detach(package:DescTools))
try(detach(package:sets))
R markdown - markdown - R code - all posts