Hi All, I am wondering if there is any examples where you can count your interested "word" in each row. For an example if you have data with *'ID*' and '*write-up*' for 100 rows, how would I calculate the word frequency for each row ? Thank you for all your time. [[alternative HTML version deleted]]
> aaa <- matrix(c("aa", "bb", "aa", "aa", "cc", "ee"), 2, 3,dimnames=list(LETTERS[1:2], letters[3:5]))> aaac d e A "aa" "aa" "cc" B "bb" "aa" "ee"> apply(aaa, 1, function(x, word) sum(x==word), word="aa")A B 2 1>On Fri, Mar 8, 2013 at 11:04 AM, Sudip Chatterjee <sudipanalyst@gmail.com>wrote:> Hi All, > > I am wondering if there is any examples where you can count your > interested "word" in each row. For an example if you have data with *'ID*' > and '*write-up*' for 100 rows, how would I calculate the word frequency for > each row ? > > Thank you for all your time. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Hello, I'm not sure I understand, but see if the following is an example of counting occurences of a word in each row. set.seed(1855) x <- matrix(sample(LETTERS[1:5], 400, replace = TRUE), ncol = 4) word <- "A" where <- apply(x, 1, function(.x) grep(word, .x)) sapply(where, length) # count them Hope this helps, Rui Barradas Em 08-03-2013 16:04, Sudip Chatterjee escreveu:> Hi All, > > I am wondering if there is any examples where you can count your > interested "word" in each row. For an example if you have data with *'ID*' > and '*write-up*' for 100 rows, how would I calculate the word frequency for > each row ? > > Thank you for all your time. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
I think the qdap package's termco (termo count) function will do what you want. Read the specifics as spacing around the word matters. ? ?? library(qdap); ? ? ?? termco(DATA$state, 1:nrow(DATA), c("it")) ? ?? ----------------------------------------> Date: Fri, 8 Mar 2013 21:34:31 +0530 > From: sudipanalyst at gmail.com > To: r-help at r-project.org > Subject: [R] Word Frequency for each row > > Hi All, > > I am wondering if there is any examples where you can count your > interested "word" in each row. For an example if you have data with *'ID*' > and '*write-up*' for 100 rows, how would I calculate the word frequency for > each row ? > > Thank you for all your time. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.