Сравнение множеств

Массивы и коллекции / Множества

first <- c(1, 2)
second <- c(2, 1)
third <- c(1, 2, 3)

# Order does not matter
is_equal <- setequal(first, second)
# is_equal = TRUE

is_intersects <- any(first %in% third)
# is_intersects = TRUE

# A subset: all its items are in the other
is_third_subset <- all(third %in% first)
# is_third_subset = FALSE

is_subset <- all(first %in% third)
# is_subset = TRUE

# Proper subset: a subset that is not equal
is_proper_subset <- all(first %in% second) &&
  !setequal(first, second)
# is_proper_subset = FALSE

cat("is_equal =", is_equal, "\n")
cat("is_intersects =", is_intersects, "\n")
cat("is_third_subset =", is_third_subset, "\n")
cat("is_subset =", is_subset, "\n")
cat("is_proper_subset =", is_proper_subset, "\n")