推特情绪循环

时间:2016-12-05 01:46:08

标签: r dataframe

我有这个循环遍历每条推文并计算该推文的情绪(见下文)。我想要做的只是将具有“负面”情绪(极性值)的推文存储到仅包含推文文本(第1列)和极性(第2列)的数据框中。我怎样才能将这些值放入循环内的数据框中(在底部)?提前感谢您提供的任何帮助。

#Packages
library(twitteR)
install.packages(c("devtools", "rjson", "bit64", "httr"))
library(devtools)
install_github("geoffjentry/twitteR")
require(devtools)
install_github('sentiment140', 'okugami79')
library(sentiment)
#Get Tweets
WalmartTweets= searchTwitter("Walmart", n = 10)
str(WalmartTweets)
    List of 10
 $ :Reference class 'status' [package "twitteR"] with 20 fields
  ..$ text           : chr "RT @FunkoDCLegion: RT & follow @FunkoDCLegion for a chance to WIN the Walmart exclusives - Classic TV Series #Batgirl Dorbz"| __truncated__
  ..$ favorited      : logi FALSE
  ..$ favoriteCount  : num 0
  ..$ replyToSN      : chr(0) 
  ..$ created        : POSIXct[1:1], format: "2016-12-05 02:03:06"
  ..$ truncated      : logi FALSE
  ..$ replyToSID     : chr(0) 
  ..$ id             : chr "805593309015994369"
  ..$ replyToUID     : chr(0) 
  ..$ statusSource   : chr "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>"
  ..$ screenName     : chr "SushiGirlLisa"
  ..$ retweetCount   : num 3333
  ..$ isRetweet      : logi TRUE
  ..$ retweeted      : logi FALSE
  ..$ longitude      : chr(0) 
  ..$ latitude       : chr(0) 
  ..$ location       : chr ""
  ..$ language       : chr "en"
  ..$ profileImageURL: chr "http://pbs.twimg.com/profile_images/2453027516/a0zdkk42kwlpo8k3xtol_normal.jpeg"
  ..$ urls           :'data.frame': 0 obs. of  4 variables:
  .. ..$ url         : chr(0) 
  .. ..$ expanded_url: chr(0) 
  .. ..$ dispaly_url : chr(0) 
  .. ..$ indices     : num(0) 
  ..and 59 methods, of which 45 are  possibly relevant:
  ..  getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLanguage, getLatitude, getLocation, getLongitude,
  ..  getProfileImageURL, getReplyToSID, getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted, getRetweeters,
  ..  getRetweets, getScreenName, getStatusSource, getText, getTruncated, getUrls, initialize, setCreated, setFavoriteCount,
  ..  setFavorited, setId, setIsRetweet, setLanguage, setLatitude, setLocation, setLongitude, setProfileImageURL,
  ..  setReplyToSID, setReplyToSN, setReplyToUID, setRetweetCount, setRetweeted, setScreenName, setStatusSource, setText,
  ..  setTruncated, setUrls, toDataFrame, toDataFrame#twitterObj


sentiments <- data.frame(Tweet= c(), polarity = c())
#Loop for sentiment of tweets
for (i in 1:length(WalmartTweets)) {
    #Compute polarity
    polarity=sentiment(WalmartTweets[[i]]$text)$polarity 
    #Store tweet and polarity in DF 
    sentiments = rbind(sentiments, list(Tweet=WalmartTweets[[i]]$text, polarity = polarity))

}
write.csv(sentiments, file = "MyData.csv")

1 个答案:

答案 0 :(得分:1)

sentiments <- data.frame(Tweet= c(), polarity = c(), stringsAsFactors=FALSE)
for (i in 1:length(WalmartTweets)) {
    #Compute polarity
    polarity=sentiment(WalmartTweets[[i]]$text)$polarity
    #Store tweet and polarity in DF 
    sentiments = rbind(sentiments, 
        list(Tweet=WalmartTweets[[i]]$text,polarity=polarity),
            stringsAsFactors=FALSE)
}