我是R的新手,我正在使用widyr进行文本挖掘。我成功地使用了找到这里的方法,在文本的每一段中得到了一个共现单词的列表,以及它们的phi系数。
守则如下:
word_cors <- review_words %>%
group_by(word) %>%
pairwise_cor(word, title, sort = TRUE) %>%
filter(correlation > .15)我知道,我还可以生成一个数据框架,其中包含共现的单词及其出现的次数,使用的代码如下:
word_pairs <- review_words %>%
pairwise_count(word, title, sort = TRUE)我需要的是一个表,它同时具有phi系数和每对单词的出现次数。我一直在研究pairwise_cor和pairwise_count,但仍然不知道如何将它们结合在一起。如果我正确理解,联接只考虑一个列进行匹配,因此我无法可靠地使用常规联接,因为在item1列中可能有多个具有相同单词的对。
这能用widyr吗?如果没有,是否还有另一套方案可以让我这样做?
以下是完整的代码:
#Load packages
pacman::p_load(XML, dplyr, stringr, rvest, httr, xml2, tidytext, tidyverse, widyr)
#Load source material
prod_reviews_df <- read_csv("SOURCE SPREADSHEET.csv")
#Split into one word per row
review_words <- prod_reviews_df %>%
unnest_tokens(word, comments, token = "words", format = "text", drop = FALSE) %>%
anti_join(stop_words, by = c("word" = "word"))
#Find phi coefficient
word_cors <- review_words %>%
group_by(word) %>%
pairwise_cor(word, title, sort = TRUE) %>%
filter(correlation > .15)
#Write data to CSV
write.csv(word_cors, "WORD CORRELATIONS.csv")我想加入pairwise_count,但是我需要它和phi系数一起。
谢谢!
发布于 2017-09-21 02:23:18
如果您正在使用整洁的数据原则和tidyverse工具,我建议您一直走下去:)并使用dplyr进行您感兴趣的联接。您可以使用left_join连接来自pairwise_cor()和pairwise_count()的计算,如果您愿意的话,您只需要从一个连接到另一个。
library(dplyr)
library(tidytext)
library(janeaustenr)
library(widyr)
austen_section_words <- austen_books() %>%
filter(book == "Pride & Prejudice") %>%
mutate(section = row_number() %/% 10) %>%
filter(section > 0) %>%
unnest_tokens(word, text) %>%
filter(!word %in% stop_words$word)
austen_section_words %>%
group_by(word) %>%
filter(n() >= 20) %>%
pairwise_cor(word, section, sort = TRUE) %>%
left_join(austen_section_words %>%
pairwise_count(word, section, sort = TRUE),
by = c("item1", "item2"))
#> # A tibble: 154,842 x 4
#> item1 item2 correlation n
#> <chr> <chr> <dbl> <dbl>
#> 1 bourgh de 0.9508501 29
#> 2 de bourgh 0.9508501 29
#> 3 pounds thousand 0.7005808 17
#> 4 thousand pounds 0.7005808 17
#> 5 william sir 0.6644719 31
#> 6 sir william 0.6644719 31
#> 7 catherine lady 0.6633048 82
#> 8 lady catherine 0.6633048 82
#> 9 forster colonel 0.6220950 27
#> 10 colonel forster 0.6220950 27
#> # ... with 154,832 more rows发布于 2017-09-20 19:00:31
我今天发现并使用了merge,它似乎使用了两个相关的列来合并数据。我不知道如何检查准确性,但我认为它是有效的。
https://stackoverflow.com/questions/46311037
复制相似问题