Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created August 16, 2013 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Protonk/6252407 to your computer and use it in GitHub Desktop.
Save Protonk/6252407 to your computer and use it in GitHub Desktop.
A smarter solution to matching many to many
# 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)
@Ironholds
Copy link

Attempting to run this over a df produces (unsurprisingly) an error. This'll be interesting ;p.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment