Count word occurrences in R

Let’s for the moment assume you wanted the number of element containing “corn”:

length(grep("corn", dataset))
[1] 3

After you get the basics of R down better you may want to look at the “tm” package.

EDIT: I realize that this time around you wanted any-“corn” but in the future you might want to get word-“corn”. Over on r-help Bill Dunlap pointed out a more compact grep pattern for gathering whole words:

grep("\\<corn\\>", dataset)

Leave a Comment