我希望这不是一个基本问题, 我有一个推文数据框(在R中)。 我的目标是按日期计算情绪。
如果有人会建议我,我将非常感激,
如何按日期汇总推文tweet$text
,其中
每个观察成为一串合并的推文/文本
例如,如果我有:
Created_Date Tweet
2014-01-04 "the iphone is magnificent"
2014-01-04 "the iphone's screen is poor"
2014-01-04 "I will always use Apple products"
2014-01-03 "iphone is overpriced, but I love it"
2014-01-03 "Siri is very sluggish"
2014-01-03 "iphone's maps app is poor compared to Android"
我想通过Created_Date合并推文的循环/函数 导致像这样的东西
Created_Date Tweet
2014-01-04 "the iphone is magnificent", "the iphone's screen is poor", "I will always use Apple products"
2014-01-03 "iphone is overpriced, but I love it", "Siri is very sluggish", "iphone's maps app is poor compared to Android"
以下是我的数据
dat <- structure(list(Created_Date = structure(c(1388793600, 1388793600,
1388793600, 1388707200, 1388707200, 1388707200), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Tweet = c("the iphone is magnificent",
"the iphone's screen is poor", "I will always use Apple products",
"iphone is overpriced, but I love it", "Siri is very sluggish",
"iphone's maps app is poor compared to Android")), .Names = c("Created_Date",
"Tweet"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-6L))
答案 0 :(得分:1)
library(tidyverse)
# this approach first use the group_by function to group by date,
# pipes `%>%` are used to pass from one data to the next with a
# transformation at each step.
ta %>%
group_by(Created_Date) %>%
summarise(cTweet = paste(Tweet, collapse = ","))
# A tibble: 2 x 2
Created_Date cTweet
<dttm> <chr>
1 2014-01-03 iphone is overpriced, but I love it,Siri is very sluggish,iphone's maps app is poor compared to Android
2 2014-01-04 the iphone is magnificent,the iphone's screen is poor,I will always use Apple products
aggregate(ta$Tweet,by=list(ta$Created_Date),FUN=function(X)paste(X, collapse = ","))
<div ng-if="products.length>0">
<select ng-model="model.newItem"
ng-options="pr.id + ') ' for pr in products track by pr.id"
ng-init="model.newItem=products[0]" />
</div>
答案 1 :(得分:0)
使用循环只是一个简单的实现。可能不是可以想象的最快的解决方案,但很容易理解。
# construction of a sample data.frame
text = c("Some random text.",
"Yet another line.",
"Will this ever stop.",
"This may be the last one.",
"It was not the last.")
date = c("9-11-2017",
"11-11-2017",
"10-11-2017",
"11-11-2017",
"10-11-2017")
tweet = data.frame(text, date)
# array with dates in the data.frame
dates = levels(tweet$date)
# initialise results with empty strings
resultString = rep.int("", length(dates))
for(i in 1:length(dates)) # loop over different dates
{
for(j in 1:length(tweet$text)) # loop over tweets
{
if (tweet$date[j] == dates[i]) # concatenate to resultString if dates match
{
resultString[i] = paste0(resultString[i], tweet$text[j])
}
}
}
# combine concatenated strings with dates in new data.frame
result = data.frame(date=dates, tweetsByDate=resultString)
result
# output:
# date tweetsByDate
# 1 10-11-2017 Will this ever stop.It was not the last.
# 2 11-11-2017 Yet another line.This may be the last one.
# 3 9-11-2017 Some random text.
答案 2 :(得分:0)
如果您使用的是语料库库,则可以使用group
参数term_counts
或term_matrix
按日期汇总(总和)。< / p>
在您的情况下,如果您有兴趣计算正面,负面和中性单词的数量,您可以先创建一个&#34; stemmer&#34;将单词映射到这些类别:
library(corpus)
# map terms in the AFINN dictionary to Positive/Negative; others to Neutral
stem_sent <- new_stemmer(sentiment_afinn$term,
ifelse(sentiment_afinn$score > 0, "Positive", "Negative"),
default = "Neutral")
然后,您可以将其用作词干分析器并按组获取计数:
term_counts(dat$Tweet, group = dat$Created_Date, stemmer = stem_sent)
## group term count
## 1 2014-01-03 Negative 2
## 2 2014-01-04 Negative 1
## 3 2014-01-03 Neutral 17
## 4 2014-01-04 Neutral 14
## 5 2014-01-03 Positive 1
或者得到一个计数矩阵:
term_matrix(dat$Tweet, group = dat$Created_Date, stemmer = stem_sent)
## 2 x 3 sparse Matrix of class "dgCMatrix"
## Negative Neutral Positive
## 2014-01-03 2 17 1
## 2014-01-04 1 14 .