Created
August 16, 2013 18:00
-
-
Save Protonk/6252065 to your computer and use it in GitHub Desktop.
The dumbest solution to the problem
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
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