On Sat, Jun 4, 2011 at 6:44 PM, Abraham Mathew <abmathe...@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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to