2021年2月5日星期五

Grepl group of strings and count frequency of all using R

I have a column of 50k rows of tweets named text from a csv file (the tweets consists of sentences, phrases etc). I'm trying to count frequency of several words in that column. Is there an easier way to do it vs what I'm doing below?

# Reading my file  tweets <- read.csv('coffee.csv', header=TRUE)      # Doing a grepl per word (This is hard because I need to look for many words one by one)  coffee    <- grepl("coffee", text$tweets, ignore.case=TRUE)  mugs    <- grepl("mugs", text$tweets, ignore.case=TRUE)      # Calculate the % of times among all tweets (This is hard because I need to calculate one by one)    sum(coffee) / nrow(text)  sum(starbucks) / nrow(text)  

Expected Output (assuming I have more than 2 words up there)

Word   Freq  coffee  50  mugs    40  cup     64  pen     12  
https://stackoverflow.com/questions/66073470/grepl-group-of-strings-and-count-frequency-of-all-using-r February 06, 2021 at 12:06PM

没有评论:

发表评论