Created
August 16, 2013 18:40
-
-
Save Protonk/6252407 to your computer and use it in GitHub Desktop.
A smarter solution to matching many to many
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For each element, match against the data frame of longer vectors | |
compSlightlySmarter <- function(element, table) { | |
matches <- sapply(table, function(x) { | |
# finds exact matches of element to the 'table' (one column in the longer df) | |
locations <- match(element, as.character(x)) | |
# if there are no unbroken or unordered sequences, it's a match! | |
return(!any(is.na(locations)) & !is.unsorted(locations)) | |
}) | |
if(any(matches)) { | |
# unlike the dumb version, we return matches | |
return(which(matches)) | |
} | |
} | |
# a list of either NULL values for no matches or the column index of the table dataframe | |
lapply(shrt, compSlightlySmarter, table = long) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempting to run this over a df produces (unsurprisingly) an error. This'll be interesting ;p.