11

所以我实际上在 twitteR 上工作,我需要一种方法将我的推文存储到 csv 文件中,并在需要时将其取出。这是因为我想编译我收集的推文,然后将它们应用于我的算法以便稍后进行计算。所以,我想试试

            write.csv(tweets, file = "newfile", row.names = TRUE, sep = ',', col.names = TRUE)

仅在创建数据框 tho :/ 时才有效。我收集的推文看起来像这样

 [[1]]
 [1] "anonymous: boring!"

 [[2]]
 [1] "anonymous: random message !"

…………

有任何想法吗?

已编辑:我的 str(tweets) 这只是我刚刚取出的 3 条推文

List of 3
 $ :Reference class 'status' [package "twitteR"] with 17 fields
  ..$ text         : chr "damn so many thing to settle @@"
  ..$ favorited    : logi FALSE
  ..$ favoriteCount: num 0
  ..$ replyToSN    : chr(0) 
  ..$ created      : POSIXct[1:1], format: "2013-10-11 14:15:59"
  ..$ truncated    : logi FALSE
  ..$ replyToSID   : chr(0) 
  ..$ id           : chr "388669309028798464"
  ..$ replyToUID   : chr(0) 
  ..$ statusSource : chr "web"
  ..$ screenName   : chr "ThisIsNapmi"
  ..$ retweetCount : num 0
  ..$ isRetweet    : logi FALSE
  ..$ retweeted    : logi FALSE
  ..$ longitude    : chr(0) 
  ..$ latitude     : chr(0) 
  ..$ urls         :'data.frame':   0 obs. of  4 variables:
  .. ..$ url         : chr(0) 
  .. ..$ expanded_url: chr(0) 
  .. ..$ dispaly_url : chr(0) 
  .. ..$ indices     : num(0) 
  ..and 50 methods, of which 38 are possibly relevant:
  ..  getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLatitude,
  ..  getLongitude, getReplyToSID, getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted,
  ..  getRetweets, getScreenName, getStatusSource, getText, getTruncated, getUrls, initialize,
  ..  setCreated, setFavoriteCount, setFavorited, setId, setIsRetweet, setLatitude,
  ..  setLongitude, setReplyToSID, setReplyToSN, setReplyToUID, setRetweetCount, setRetweeted,
  ..  setScreenName, setStatusSource, setText, setTruncated, setUrls, toDataFrame,
  ..  toDataFrame#twitterObj
 $ :Reference class 'status' [package "twitteR"] with 17 fields
  ..$ text         : chr "@Neverush @asmafab http://t.co/TOakKW4kyc"
  ..$ favorited    : logi FALSE
  ..$ favoriteCount: num 0
  ..$ replyToSN    : chr "Neverush"
  ..$ created      : POSIXct[1:1], format: "2013-10-11 12:55:04"
  ..$ truncated    : logi FALSE
  ..$ replyToSID   : chr "388647414808051712"
  ..$ id           : chr "388648948111392770"
  ..$ replyToUID   : chr "44332730"
  ..$ statusSource : chr "web"
  ..$ screenName   : chr "ThisIsNapmi"
  ..$ retweetCount : num 0
  ..$ isRetweet    : logi FALSE
  ..$ retweeted    : logi FALSE
  ..$ longitude    : chr(0) 
  ..$ latitude     : chr(0) 
  ..$ urls         :'data.frame':   1 obs. of  5 variables:
  .. ..$ url         : chr "http://t.co/TOakKW4kyc"
  .. ..$ expanded_url: chr "http://www.youtube.com/watch?v=2mjvfnUAfyo"
  .. ..$ display_url : chr "youtube.com/watch?v=2mjvfn…""| __truncated__
  .. ..$ start_index : num 19
  .. ..$ stop_index  : num 41
  ..and 50 methods, of which 38 are possibly relevant:
  ..  getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLatitude,
  ..  getLongitude, getReplyToSID, getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted,
  ..  getRetweets, getScreenName, getStatusSource, getText, getTruncated, getUrls, initialize,
  ..  setCreated, setFavoriteCount, setFavorited, setId, setIsRetweet, setLatitude,
  ..  setLongitude, setReplyToSID, setReplyToSN, setReplyToUID, setRetweetCount, setRetweeted,
  ..  setScreenName, setStatusSource, setText, setTruncated, setUrls, toDataFrame,
  ..  toDataFrame#twitterObj
 $ :Reference class 'status' [package "twitteR"] with 17 fields
  ..$ text         : chr "@Neverush @asmafab nasi lemak bumbung ? ahahahaha"
  ..$ favorited    : logi FALSE
  ..$ favoriteCount: num 0
  ..$ replyToSN    : chr "Neverush"
  ..$ created      : POSIXct[1:1], format: "2013-10-11 12:34:39"
  ..$ truncated    : logi FALSE
  ..$ replyToSID   : chr "388643321108631552"
  ..$ id           : chr "388643810613264384"
  ..$ replyToUID   : chr "44332730"
  ..$ statusSource : chr "web"
  ..$ screenName   : chr "ThisIsNapmi"
  ..$ retweetCount : num 0
  ..$ isRetweet    : logi FALSE
  ..$ retweeted    : logi FALSE
  ..$ longitude    : chr(0) 
  ..$ latitude     : chr(0) 
  ..$ urls         :'data.frame':   0 obs. of  4 variables:
  .. ..$ url         : chr(0) 
  .. ..$ expanded_url: chr(0) 
  .. ..$ dispaly_url : chr(0) 
  .. ..$ indices     : num(0) 
  ..and 50 methods, of which 38 are possibly relevant:
  ..  getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLatitude,
  ..  getLongitude, getReplyToSID, getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted,
  ..  getRetweets, getScreenName, getStatusSource, getText, getTruncated, getUrls, initialize,
  ..  setCreated, setFavoriteCount, setFavorited, setId, setIsRetweet, setLatitude,
  ..  setLongitude, setReplyToSID, setReplyToSN, setReplyToUID, setRetweetCount, setRetweeted,
  ..  setScreenName, setStatusSource, setText, setTruncated, setUrls, toDataFrame,
  ..  toDataFrame#twitterObj
4

3 回答 3

10

您可以使用以下内容将推文转换为推文数据框:

tweets.df <- do.call("rbind", lapply(tweets, as.data.frame)) 

然后在您的 write.csv 函数中使用 tweets.df。

于 2015-04-02T03:38:20.537 回答
10

未经测试,但根据我在网上阅读的内容,似乎以下内容应该有效:

  1. 将 转换listdata.frame

    library(plyr) 
    tweets.df = ldply(tweets, function(t) t$toDataFrame())
    
  2. 像以前一样使用write.csv,但只是在tweets.df对象上而不是在tweets对象上。

    write.csv(tweets.df, file = "newfile.csv")
    

资料来源:这里这里。另见:?"status-class"

于 2013-10-12T06:19:14.777 回答
0

使用推特包:

将您的推文转换为数据框

tweets2df <- twListToDF(tweets)

然后将其保存到 csv

write.csv(tweets2df, file = "tweets.csv")
于 2016-11-01T18:45:14.853 回答