Word count by a category in a dataframe in R -


i have column text reviews, , column ratings:

content                rating  "bluetooth bad"          1  "head unit crashes"         2  "remote works awesome"      5 

i want input set of keywords, , count occurrence in comments, different ratings.

put simply, find out different people (rating defines cohort) mention most.

rating    word    count  1     bluetooth   1  1     head unit   0  1     remote      0  2     bluetooth   0  2     head unit   1  2     remote      0  5     bluetooth   0  5     head unit   0  5     remote      1 

i coding after many years, , frankly trying write function have many syntactical errors.

i think we're after. can call function instance of word passed , count number of ratings.

library(data.table) dt = data.table(content = c("bluetooth bad", "head unit crashes", "remote workds awesome", "bluetooth ok..."), rating = c(1,2,5,3))  > dt                  content rating 1:      bluetooth bad      1 2:     head unit crashes      2 3: remote workds awesome      5 4:    bluetooth ok...      3  count = function(word, dt){    dt = dt[grepl(word, content, ignore.case = true), .(count = .n), = .(rating)]   dt[ , content := word]   print(dt) } 

then, can @ counts bluetooth

count("bluetooth", dt)    rating count   content 1:      1     1 bluetooth 2:      3     1 bluetooth 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -