2016-12-05 46 views
-1

我有這個循環遍歷每條推文並計算該推文的情緒(見下文)。我想要做的只是將只包含鳴叫文本(第1列)和極性(第2列)的數據框存儲爲具有「負面」情緒(極性值)的鳴叫。我怎樣才能將這些值放入循環內的數據框中(在底部)?預先感謝您提供的任何幫助。Tweet Sentiment Loop

#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") 
+0

該早點起牀關閉,因爲它是不可重複的。給出'WalmartTweets'的一些示例數據以及'sentiment'函數來自的包。 http://stackoverflow.com/questions/40961229/adding-negative-tweets-into-a-dataframe-in-r這一個會再次關閉,而沒有采取行動......而你沒有閱讀那裏的那個問你的評論閱讀SO可重現的示例指南:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610 –

+0

添加到該職位的上下文 – bkubs557

回答

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) 
} 
+0

這有效,但它只出口1行到我的CSV文件。索引將包含所有10行? (請參閱底部的更新代碼)。 – bkubs557

+0

@ bkubs557我看到你修改了一些類似於我的答案的問題,但問題在於你分離出了情感1和情感2。只要保留它就好了(只是情緒 - 不是1或2)。它會重複更新相同的數據幀。 – G5W

+0

另外,我看到一個錯字。你有一個地方,你有兩個l極性 – G5W