将位置转换为页面,然后计算每个页面中的平均情绪。按页绘制平均分数

Convert the locations to pages and then compute the average sentiment in each page. Plot that average score by page

需要有关 9 的帮助。但我已经包含了相关代码。

5. 稍后我们将在书中绘制情绪与位置的关系图。为此,在 table 中添加一个包含单词 number 的列会很有用。
words <- words %>% 
  mutate(word_num = 1:length(word))
head(words)
6. 从词对象中删除停用词和数字。提示:使用 anti_join。
data("stop_words")
words_clean <- words %>% 
  anti_join(stop_words) %>% 
  filter(!str_detect(word,"^\d+$"))
head(words_clean)
7. 现在使用 AFINN 词典为每个词分配一个情感值。
library(textdata)
afinn <- get_sentiments("afinn")
words_sentiments <- words_clean %>% 
  inner_join(afinn, by = "word")  
head(words_sentiments)
8. 绘制情绪得分与书中位置的关系图,并添加平滑度。
library(ggplot2)
words_sentiments %>% ggplot(aes(x= word_num, y=value)) +
  geom_point()+
  geom_smooth()
  
  
  
9. 假设每页有 300 字。将位置转换为页面,然后计算每个页面中的平均情绪。按页绘制平均分数。添加一个似乎可以通过数据的平滑器。
# word_sentiments2 <- words_sentiments %>% 
  cut(words_sentiments$word_num,seq(1,max(words_sentiments$word_num), by=300))
word

为了得到 page_number 我们使用商的上限,然后我们 group_by page_number 并汇总值

words_sentiments %>%
  mutate(page_number = ceiling(word_num / 300)) %>%
   group_by(page_number) %>%
    summarise(average=mean(value)) -> summed
summed %>%  ggplot(aes(x=page_number, y=average)) + geom_point() + geom_smooth()