-1

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")
bhat557
  • 105
  • 2
  • 11
  • This got closed earlier because it wasn't reproducible. Give some sample data for `WalmartTweets` and the packages where `sentiment` function comes from. http://stackoverflow.com/questions/40961229/adding-negative-tweets-into-a-dataframe-in-r This one will get closed again without action...and you didn't read the comment there that asked you to read SO guidelines for reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610 – Tyler Rinker Dec 05 '16 at 01:54
  • Added context to the post – bhat557 Dec 05 '16 at 02:06

1 Answers1

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)
}
G5W
  • 36,531
  • 10
  • 47
  • 80
  • That works but it only exports 1 row to my csv file. Where would the index go to include all 10 rows? (see updated code at the bottom). – bhat557 Dec 05 '16 at 02:35
  • @bkubs557 I see that you modified the question something like my answer, But the problem is that you separated out sentiments1 and sentiments2. Just leave it as I had it (just sentiments - no 1 or 2) . it will repeatedly update the same data.frame. – G5W Dec 05 '16 at 02:39
  • Also, I see a typo. You have one place that you have two l's in polarity – G5W Dec 05 '16 at 02:40
  • Thanks, the logic seems to be working correctly but only 1 tweet keeps repeating itself and "NA" is filled in the csv where the other tweets/polarity should be (see update). – bhat557 Dec 05 '16 at 03:01
  • @bkubs557 I think that I see the problem. Hang on a few minutes – G5W Dec 05 '16 at 03:08
  • @bkubs557 I edited my solution. Note that I added `stringsAsFactors=FALSE` in two places. Give that a try. – G5W Dec 05 '16 at 03:15
  • Perfect, you are a life saver! – bhat557 Dec 05 '16 at 03:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129768/discussion-between-g5w-and-bkubs557). – G5W Dec 05 '16 at 03:43