Bing 整洁极性:统计并透视白鲸
在本练习中,您将使用 "bing" 词典再做一次 inner_join()。
随后,您将结合使用来自 dplyr 的 count() 和来自 tidyr 的 pivot_wider() 来整理并理解文本结果。
pivot_wider() 会将数据"铺开"到多列。本例中,情感类别以及对应的 n 值表示每一行中正面或负面术语的出现频次。使用 pivot_wider() 后,数据会变为每一行同时包含正面和负面两个数值,即使其中某个为 0。
本练习是课程的一部分
R 中的情感分析
练习说明
在此练习中,您的 R 会话中已提供 m_dick_tidy(包含《白鲸》一书的内容)和 bing(与上一个练习相同的词典)。
- 对
m_dick_tidy与bing执行一次inner_join()。- 与先前相同,将
m_dick_tidy中的"term"列与词典中的"word"列进行连接。 - 将新对象命名为
moby_lex_words。
- 与先前相同,将
- 创建一列
index,其值为对document应用as.numeric()的结果。请在 tidyverse 的mutate()中完成。 - 将
moby_lex_words传递给count(),按sentiment, index统计,生成moby_count。 - 通过将
moby_count管道传入pivot_wider()生成moby_wide,其中names_from等于sentiment列,values_from等于n列,并使用values_fill = 0填充缺失值。 - 接着使用
arrange按index值对行进行排序。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Inner join
moby_lex_words <- inner_join(___, ___, by = c("___" = "___"))
moby_lex_words <- moby_lex_words %>%
# Set index to numeric document
mutate(___ = as.numeric(___))
moby_count <- moby_lex_words %>%
# Count by sentiment, index
___(___, ___)
# Examine the counts
moby_count
moby_wide <- moby_count %>%
# Pivot the sentiments
pivot_wider(names_from = ___, values_from = ___, values_fill = ___) %>%
arrange(index)
# Review the pivoted data
moby_wide