0

I'm doing text analysis using tidytext. I am trying to calculate the tf-idf for a corpus. The standard way to do this is:

book_words <- book_words %>%
   bind_tf_idf(word, book, n)

However, in my case, the 'document' is not defined by a single column (like book). Is it possible to call bind_tf_idf where the document is defined by two columns (for example, book and chapter)?

4

1 回答 1

3

为什么不连接两列?例如

library(tidyverse)
library(tidytext)
library(janeaustenr)
book_words <- austen_books() %>%
  unnest_tokens(word, text) %>%
  count(book, word, sort = TRUE) %>%
  ungroup()
book_words$chapter <- sample(1:10, nrow(book_words), T)
book_words %>%
  unite("book_chapter", book, chapter) %>%
  bind_tf_idf(word, book_chapter, n) %>% print %>%
  separate(book_chapter, c("book", "chapter"), sep="_") %>% 
  arrange(desc(tf_idf))
于 2017-05-08T15:38:36.660 回答