2017-05-08 1 views
0

tidytext을 사용하여 텍스트 분석을하고 있습니다. 코퍼스의 tf-idf를 계산하려고합니다. 이 작업을 수행하는 표준 방법은 다음과 같습니다문서가 두 개의 열로 정의되는 경우 tf idf 가져 오기

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

하지만, 내 경우, '문서'하나의 열 (같은이 book)에 의해 정의되어 있지 않습니다. 문서가 두 개의 열 (예 : bookchapter)으로 정의되는 bind_tf_idf를 호출 할 수 있습니까?

+0

잘 모르겠습니다. 하나의 텍스트 열을 만들기 위해 두 열을 함께 바인딩 할 수 없습니까? 예 : cbind (책, 장) – triddle

답변

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))