【问题标题】:How to use a month index in a count function in R using Pipr如何使用管道在 R 中的计数函数中使用月份索引
【发布时间】:2020-12-01 11:23:30
【问题描述】:

我有以下问题:我有一个包含 3 列、行号、日期和一个单词的数据框。我正在尝试使用 https://www.tidytextmining.com/ 方法对 GitHub 提交 cmets 执行文本分析。我希望按季度计算我的总体情绪得分,而不是按我通过count(index = line %/% 10, sentiment) %>% 所做的 cmets 的数量。有没有一种简单的方法可以按季度计算我所有的“情绪分数”?

非常感谢您的任何建议。

single_word_with_date$date <- substr(single_word_with_date$date,1,nchar(single_word_with_date$date)-10)

single_word_with_date$date <- as.Date(single_word_with_date$date , format = "%Y-%m-%d")

comment_sentiments_with_date <- single_word_with_date %>%
  inner_join(get_sentiments("bing")) %>%
  count(index = date %/% month(date) , sentiment) %>%
  spread(sentiment, n, fill = 0) %>%
  mutate(sentiment = positive - negative)

这是数据框行是注释编号(例如,第 2 行注释中有几个单词),日期是日期时间,单词是字符串。

> head(single_word_with_date)
     line       date     word
1       1 2011-11-16     love
2       2 2012-04-13   random
2.1     2 2012-04-13 question
2.8     2 2012-04-13 answered
2.14    2 2012-04-13   darwin
2.19    2 2012-04-13  purpose

【问题讨论】:

标签: r dplyr tidyr


【解决方案1】:

试试这个:

library(tidytext)
library(dplyr)
library(tidyr)

single_word_with_date %>%
  inner_join(get_sentiments("bing")) %>%
  group_by(quarter = paste(format(date, '%Y'), quarters(date), sep = '-')) %>%
  pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
  mutate(sentiment = positive - negative) -> result

result

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多