I have this loop that goes over each tweet and calculates the sentiment of that tweet (see below). What I want to do is store only the tweets that have a "negative" sentiment (polarity value) into a dataframe that contains only the tweet text (column 1) and the polarity (column 2). How could I get those values into a dataframe within the loop (at the bottom)? Thanks in advance for any help provided.
#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")