unique()
removes duplicate elements of a vector, and duplicate rows of an array.
is.element()
, %in%
, and match()
works only on vectors (or NULL).
Are there any value matching or set operations for multiple variables? (e.g. rows of an array)
My current workaround is this. It's not quite elegant, and it's obviously sensitive to "_" matching.
match.multiple <- function (x, table, nomatch = NA_integer_, incomparables = NULL) {
x_vector <- apply(x, 1, paste, collapse="_")
table_vector <- apply(table, 1, paste, collapse="_")
match(x_vector, table_vector, nomatch, incomparables)}
is.element.multiple <- "%in.multiple%" <- function (el, set) match.multiple(el, set, 0) > 0
Edit: adding a reproducible example
Lets say that you wish to buy a car which has an equal number of forward gears and carburetors. It can be 1-each, 2-each etc. You don't know whether the cars that are available on the market (cf. mtcars
) comply with your preferences.
preferences <- cbind(1:8, 1:8)
available <- cbind(mtcars$gear, mtcars$carb)
So you do a matching for both variables: gears and carburetors.
m <- match.multiple(preferences, available)
m
# [1] NA NA 12 1 NA NA NA NA
which(!is.na(m))
# [1] 3 4
These are the number of forward gears and carburetors which come in equal quantities.
willbuy <- m[!is.na(m)]
mtcars[willbuy, ]
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1: 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
# 2: 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
And these are catalogue entries for cars that you should consider.