1

I've been trying to calculate word frequencies with the tidytext package.

v <- "Everybody dance now! Give me the music Everybody dance now! Give me the music Everybody dance now! Everybody dance now! Yeah! Yeah! Yeah!"
v <- as.character(v)
v %>% count(words)

but I've been getting this error: Error in UseMethod("as.quoted") : no applicable method for 'as.quoted' applied to an object of class "function"

please help! thanks!

Vickie Ip
  • 183
  • 1
  • 1
  • 5
  • I don't think there's a `count` method in tidytext by itself. tidytext can help you turn a document or string into a dataframe with words that you can count, tho. See answer below. – twedl Feb 02 '18 at 19:58

2 Answers2

3

tidytext is the package that lets you convert strings (in dataframes) into words and other things. You can convert your string to a dataframe and then use the tidytext method unnest_tokens to convert it into words, and then use dplyr to group_by the words and then count them:

tibble(v) %>% tidytext::unnest_tokens(word, v) %>% group_by(word) %>% count()
# A tibble: 8 x 2
# Groups:   word [8]
  word          n
  <chr>     <int>
1 dance         4
2 everybody     4
3 give          2
4 me            2
5 music         2
6 now           4
7 the           2
8 yeah          3
twedl
  • 1,588
  • 1
  • 17
  • 28
0

I'm working on a similar case and calling dplyr worked with the count() function:

tokens %>%
# call dplyr   
dplyr::count(word)