match_n_to_m {reclin2} | R Documentation |
Force n to m matching on a set of pairs
Description
Force n to m matching on a set of pairs
Usage
match_n_to_m(x, y, w, n = 1, m = 1)
Arguments
x |
a vector of identifiers for each x in each pair This vector should have a unique value for each element in x. |
y |
a vector of identifiers for each y in each pair This vector should have a unique value for each element in y. |
w |
a vector with weights for each pair. The algorithm will try to maximise the total weight of the selected pairs. |
n |
an integer. Each element of x can be linked to at most n elements of y. |
m |
an integer. Each element of y can be linked to at most m elements of x. |
Details
The algorithm will try to select pairs in such a way each element of x
is matched to at most n
elements of y
and that each element of
y
is matched at most m
elements of x
. It tries to select
elements in such a way that the total weight w
of the selected
elements is maximised.
Value
A logical vector with the same length as x
indicating the selected
records.
Examples
d <- data.frame(x=c(1,1,1,2,2,3,3), y=c(1,2,3,4,5,6,7), w=1:7)
# One-to-one matching:
d[match_n_to_m(d$x, d$y, d$w), ]
# N-to-one matching:
d[match_n_to_m(d$x, d$y, d$w, n=999), ]
# One-to-m matching:
d[match_n_to_m(d$x, d$y, d$w, m=999), ]
# N-to-M matching, e.g. select all pairs
d[match_n_to_m(d$x, d$y, d$w, n=999, m=999), ]