I\'d like to check whether two vectors contain the same elements, even if they\'re not ordered the same. For example, the function (let\'s call it SameElements)
SameElements
In lieu of a cleaner alternative, here's the known solution:
SameElements <- function(a, b) return(identical(sort(a), sort(b))) SameElements(c(1, 2, 3), c(1, 3, 2)) # TRUE SameElements(c(1, 2, 3), c(1, 1, 3, 2)) # FALSE
Edit: identical instead of all.equal(...) == T per nrussell's suggestion.
identical
all.equal(...) == T