我正在尝试跟随这个关于情感分析的在线教程(链接)。以下是代码:
new_sentiments <- sentiments %>% #From the tidytext package
filter(lexicon != "loughran") %>% #Remove the finance lexicon
mutate( sentiment = ifelse(lexicon == "AFINN" & score >= 0, "positive",
ifelse(lexicon == "AFINN" & score < 0,
"negative", sentiment))) %>%
group_by(lexicon) %>%
mutate(words_in_lexicon = n_distinct(word)) %>%
ungroup()
生成错误:
>Error in filter_impl(.data, quo) :
>Evaluation error: object 'lexicon' not found.
也许与此相关的是,对我来说,“情感”表格似乎出现了异常(损坏?)。这是“情感”表格的表头:
> head(sentiments,3)
> element_id sentence_id word_count sentiment
> chapter
> 1 1 1 7 0 The First Book of Moses:
> Called Genesis
> 2 2 1 NA 0 The First Book of Moses:
> Called Genesis
> 3 3 1 NA 0 The First Book of Moses: >
> Called Genesis
> category
> 1 The First Book of Moses: Called Genesis
> 2 The First Book of Moses: Called Genesis
> 3 The First Book of Moses: Called Genesis
如果我使用Get_Sentiments来获取Bing、AFINN或NRC的情感分析结果,我会得到看起来合适的响应。
> get_sentiments("bing")
> # A tibble: 6,788 x 2
> word sentiment
> <chr> <chr> > 1 2-faced negative
> 2 2-faces negative
> 3 a+ positive
> 4 abnormal negative
我尝试了删除(remove.packages)并重新安装tidytext,但行为没有改变。 我正在运行R 3.5。
即使我完全误解了问题,我也会感谢任何人能提供给我的见解。
stats::filter
而不是dplyr::filter
时,我看到了这个错误。也许library(dplyr)
可以帮助解决问题? - r2evansfilter_impl
错误可能是因为sentiments
不是一个data.frame
或者 它没有一个名为lexicon
的列。str(sentiments)
的输出是什么样子的? - r2evanssentiments
猜测这是问题的一部分,如果不是全部的话。如何解决? - KEAppleby