Created
August 26, 2014 21:14
-
-
Save emhart/0c374e66860d02a67b4f to your computer and use it in GitHub Desktop.
sapply gone awry
This file contains hidden or 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
# sapply gone terribly terribly wrong, or used perfectly, I can't tell. | |
# Use: | |
# input: a vector of words, and I want to know are any of the words in input are in the vector out | |
# out: a vector of words that I want to check against, a master list of words | |
# I then want to return an index of TRUE/FALSE to determine if a word is in in the master list | |
input <- c("Lorem","ipsum","testing") | |
out <- c("Loremipsum","qwerty","keyboard","dvorak") | |
dindex <- sapply(input, function(x,y){ind <- grep(x,y);ifelse(length(ind) > 0, for(i in ind){ifelse(x == y[i],return(TRUE),return(FALSE))},return(FALSE))},y=out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's another crack for partial matching. The
stringi
library has some very fast string search implementations. Usestri_detect_regex
if you're doing regex matches.