| multimatch | R Documentation |
Like match, but for more than one variable at a time— and geared specifically to dataframes (or matrices). NA values match only to NAs.
So useful that I've finally moved it from secret package handy2 into package mvbutils, and added documentation.
Any factor fields (which I hardly ever use; characters are just Better) will be matched based on the strings they display as, so that (eg) arbitrary re-orderings levels won't matter.
multimatch(df1, df2, nomatch = NA, char.force = FALSE, force.same.cols = TRUE)
df1, df2 |
two dataframes. Unless you set |
nomatch |
like in |
char.force |
?convert all columns to |
force.same.cols |
Perhaps a misleading name... set to |
multimatch works by constructing a single numeric composite for each row in df1 and df2, based on multiplying numbers of distinct values across columns. This could potentially overflow, or give inaccurate results, if the number of columns and distinct values is very large. So, don't use multimatch in that situation...
A numeric vector, one element per row in df1, showing which row in df2 it matches to, or nomatch if none do.
match
xx <- data.frame(
animal= cq( cat, dog, cat), colour= cq( blue, blue, pink), size= 1:3,
royalty= cq( high, low, high))
yy <- data.frame(
animal= cq( dog, dog, cat), colour= cq( red, blue, pink), size= 1:3,
loyalty=cq( high, high, low)) # note the spelling!
multimatch( xx, yy)
# NA NA NA
multimatch( xx[,1:3], yy[,1:3]) # ignore 4th col
# NA 2 3
multimatch( xx, yy, force=FALSE) # auto-drop loyalty & royalty (different names)
# NA 2 3
try( multimatch( xx[,1:2], yy[,1:3]))
# <error>: num of cols
try( multimatch( xx[,1:2], yy[,1:3], force=FALSE))
# all good
multimatch( as.matrix( xx[,1:3]), as.matrix( yy[,1:3])) # matrices OK too
# NA 2 3
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.