Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created August 16, 2013 18:00
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/6252065 to your computer and use it in GitHub Desktop.
Save Protonk/6252065 to your computer and use it in GitHub Desktop.
The dumbest solution to the problem
long <- data.frame(c("foo","qux","baz","quux"),c("baz","bar","foo","corge"))
shrt <- data.frame(c("foo","bar","baz"),c("baz","bar","foo"))
concatDumb <- function(data) {
listed <- sapply(data, function(x) {
# concatenate the character vectors to use substring matching
paste0('$', paste(unname(x), collapse = '$'), '$')
})
return(unname(listed))
}
compDumb <- function(short, long) {
# use pmatch to avoid odd behavior for duplicate matches
matches <- !is.na(pmatch(concatDumb(short), concatDumb(long)))
if(any(matches)) {
# always return a data frame
return(long[, matches, drop = FALSE])
} else {
print('No matches')
}
}
compDumb(shrt, long)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment