Count number of times a word appears in each row and store in new column (dplyr)

297 Views Asked by At

I have a character vector containing basically paragraphs of words. I would like to count the number of times a specific word appears in each row separately and then create a new vector to hold this number. How can I achieve this with dplyr? (Any other method available is also okay).

The closest I've come to a solution is on this link: Count number of times a word appears (dplyr) but it's not giving me exactly what I want.

1

There are 1 best solutions below

0
On

You can split the paragraphs by their separator and sum the conditional check:

df <- structure(list(words = c("CDjointdisease state glasses CDdiabetes eyesight", 
                               "accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis"
)), .Names = c("words"), row.names = 1:2, class = "data.frame")


> df
                                                                      words
1                          CDjointdisease state glasses CDdiabetes eyesight
2     accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis

df$count <- sapply(strsplit(df$words, " "), function(x){
  sum(x == "eyesight")
})

> df
                                                                      words count
1                          CDjointdisease state glasses CDdiabetes eyesight     1
2     accidents_combined docvisits4w citysize CDliverdisease CDosteoporosis     0