> Hi - a very simply question, but I'm hung up on it.
>
> I have a vector (nouns) with a list of words I want to search for.
>
> for (j in nouns){
> output<-grep(j, student.lines.2, value=TRUE)}
>
> This works, but it finds parts of words that match as well as whole
> words. I need to limit it to whole words.
You'll want to use perl-style regexes and make use of the word boundary
escape sequence. Something like (untested)
for (j in nouns){
output<-grep(paste("\b", j, "\b", sep=""),
student.lines.2,
value=TRUE, perl=TRUE)
}
HTH,
/au
--
Austin Frank
http://aufrank.net
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc
# example data
nouns<-c("car", "truck", "bike")
corpus<-c("I need to take care of my bike and my truck")
# loop and paste
for (j in nouns) {
output<-grep(paste("\\b", j, "\\b", sep=""), corpus, perl=T ,value=T)
cat("Search term:", j, "\t\toutput:\t", output, "\n", sep="")
}
HTH,
STG
--
Stefan Th. Gries
-----------------------------------------------
University of California, Santa Barbara
http://www.linguistics.ucsb.edu/faculty/stgries
-----------------------------------------------