[R] Partial Matching

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 5 04:10:39 CEST 2011


On Sat, Jun 4, 2011 at 6:44 PM, Abraham Mathew <abmathewks at gmail.com> wrote:
> Let's say that I have a string and I want to know if a single word
> is present in the string. I've written the following function to see if
> the word "Geico" is mentioned in the string "Cheap Geico car insurance".
> However, it doesn't work, and I assume it has something to do with the any()
> function. Do I need to use regular expressions? (I hope not)
>
> main <- function(keyword){
>       for( i in keyword ){
>            n = strsplit(as.character(keyword), " ")
>            print( n )
>            if( any( n=="Geico" )){
>                 print( "Yes" )
>          }
>     }
> }
>
> main("Cheap Geico car insurance")
>

strsplit returns a one component list containing the vector of words
so you want to replace the relevant statement with:

n = strsplit(as.character(keyword), " ")[[1]]

however, regular expressions is shorter:

> x <- c("Cheap Geico car insurance", "Cheap Gorilla car insurance", "A Geicor car")
> regexpr("\\bGeico\\b", x) > 0
[1]  TRUE FALSE FALSE

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list