r - 使用 Twitter Api 生成表加入 Sentimentr 表
问题描述
我正在使用 Trinker 的情感词典根据关键字对推文进行情感分析,我想知道,如何将我的推文表与所有名为 Twitter_list 的推特信息与我的名为 Sentiment_Scores 的情感得分表一起加入?
library(Sentimentr)
library(twitteR)
# SEARCH QUERY
tweets <- searchTwitter("Starbucks", n=1000,lang = "en")
#convert to dataframe
Twitter_list <- twListToDF(tweets)
Output:
text favorited favoritedCount Created
1 Starbucks TRUE 2 2019-01-12
2 starbucks is the FALSE 2 2019-01-11
3 starbucks sucks FALSE 1 2019-01-10
4 Iced starbucks rules FALSE 1 2019-01-09
5 I love starbucks so much True 3 2019-01-08
# Sentiment Score of Text
Sentiment_Scores <- sentiment(Twitter_list$text)
Output:
element_id Sentance_id Word_Count Sentiment
1 1 1 2 0.1856953
2 2 1 5 0.1856953
3 3 1 2 0.1856953
4 4 1 1 0.1856953
5 5 1 3 0.1856953
理想情况下,我希望输出看起来像
text favorited favoritedCount Created Word_Count Sentiment
1 Starbucks TRUE 2 2019-01-12 1 0.1856953
2 starbucks is the FALSE 2 2019-01-11 3 0.1856953
3 starbucks sucks FALSE 1 2019-01-10 2 0.1856953
4 Iced starbucks rules FALSE 1 2019-01-09 3 0.1856953
5 I love starbucks so much True 3 2019-01-08 5 0.1856953
解决方案
这个?Tweets 是你的tweets
data.frame
whilesents
是sentiment_scores
。
library(tidyverse)
new_tweets<-tweets %>%
mutate(ID=as.factor(row_number()))
new_sents<-sents %>%
mutate(ID=as.factor(row_number()))
merged<-new_tweets %>%
left_join(new_sents,by="ID") %>%
select(-ID)
merged
您可以将它们与cbind
first 合并并检查。
new_tweets<-new_tweets[,-ncol(new_tweets)]
combined<-cbind(new_tweets,new_sents)
as.tibble(combined)
MyText favorited favoritedCount Created element_id Sentance_id Word_Count Sentiment ID
<fct> <lgl> <int> <fct> <int> <int> <int> <dbl> <fct>
1 Starbucks TRUE 2 2019-0~ 1 1 2 0.186 1
2 starbucks~ FALSE 2 2019-0~ 2 1 5 0.186 2
3 starbucks~ FALSE 1 2019-0~ 3 1 2 0.186 3
4 Icedstarb~ FALSE 1 2019-0~ 4 1 1 0.186 4
5 Ilovestar~ TRUE 3 2019-0~ 5 1 3 0.186 5
推荐阅读
- python - 无法使用以下代码引发 SystemExit
- javascript - 将 Div 保存为 JPG 保存但某些内容为空/空白
- java - 在结果集中找不到列
- html - 将 HTML 内容从 SQL 数据库移动到 XML 文件以进行 Wordpress 导入
- openstreetmap - 如何计算一个节点出现的总次数是一种方式的集合?
- c# - C#:我的应用程序是否可以共享在我的其他类之一中触发的事件?
- typescript - 提取自己的类属性/成员类型
- sql - Oracle - 缺少左括号
- android - 如何通过BluetoothGatt在两部Android手机中相互通信
- upc - 如何解决 UPC 运行时错误:共享内存不足