HI,
library(stringr) dat1<- read.table(text=" ID, Data 1,I love oranges and apples 2,Oranges are good so we must eat them and also drik oranges 3,Apples are not that good like oranges so eat oranges ",sep=",",header=TRUE,stringsAsFactors=FALSE) #If you need to count both "Oranges" and "oranges" str_count(tolower(dat1$Data),"oranges") #[1] 1 2 2 #otherwise str_count(dat1$Data,"oranges") #[1] 1 1 2 A.K. ________________________________ From: Sudip Chatterjee <sudipanal...@gmail.com> To: Rui Barradas <ruipbarra...@sapo.pt> Cc: arun <smartpink...@yahoo.com>; R help <r-help@r-project.org> Sent: Saturday, March 9, 2013 11:01 AM Subject: Re: [R] Word Frequency for each row Hi All, This is very helpful, but my data looks like this ID Data 1 I love oranges and apples 2 Oranges are good so we must eat them and also drik oranges 3 Apples are not that good like oranges so eat oranges & I would like to count # of oranges in each row. How do I go about it ? Warm Regards On Sat, Mar 9, 2013 at 4:20 AM, Rui Barradas <ruipbarra...@sapo.pt> wrote: Hello, > >I had thought of something like that, but I'm not sure if the match must be >exact. If not, grep seems better. More complicated and slower but more >flexible. > >Rui Barradas > >Em 08-03-2013 21:32, arun escreveu: > > > >> >>Hi, >>You can also try: >> res2<-rowSums(x==word) >> >>res1<-sapply(where,length) >>res1[]<- sapply(res1,as.numeric) >> identical(res1,res2) >>#[1] TRUE >>A.K. >> >> >> >>----- Original Message ----- >>From: Rui Barradas <ruipbarra...@sapo.pt> >>To: Sudip Chatterjee <sudipanal...@gmail.com> >>Cc: r-help@r-project.org >>Sent: Friday, March 8, 2013 4:26 PM >>Subject: Re: [R] Word Frequency for each row >> >>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@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. >>> >>> >>______________________________________________ >>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. >> >> ______________________________________________ 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.