How to convert searchTwitter results (from library(twitteR)) into a data.frame?

前端 未结 6 1219
鱼传尺愫
鱼传尺愫 2020-12-28 20:50

I am working on saving twitter search results into a database (SQL Server) and am getting an error when I pull the search results from twitteR.

If I execute:

<
6条回答
  •  渐次进展
    2020-12-28 21:34

    Try this:

    ldply(searchTwitter("#rstats", n=100), text)
    

    twitteR returns an S4 class, so you need to either use one of its helper functions, or deal directly with its slots. You can see the slots by using unclass(), for instance:

    unclass(searchTwitter("#rstats", n=100)[[1]])
    

    These slots can be accessed directly as I do above by using the related functions (from the twitteR help: ?statusSource):

     text Returns the text of the status
     favorited Returns the favorited information for the status
     replyToSN Returns the replyToSN slot for this status
     created Retrieves the creation time of this status
     truncated Returns the truncated information for this status
     replyToSID Returns the replyToSID slot for this status
     id Returns the id of this status
     replyToUID Returns the replyToUID slot for this status
     statusSource Returns the status source for this status
    

    As I mentioned, it's my understanding that you will have to specify each of these fields yourself in the output. Here's an example using two of the fields:

    > head(ldply(searchTwitter("#rstats", n=100), 
            function(x) data.frame(text=text(x), favorited=favorited(x))))
                                                                                                                                              text
    1                                                     @statalgo how does that actually work? does it share mem between #rstats and postgresql?
    2                                   @jaredlander Have you looked at PL/R? You can call #rstats from PostgreSQL: http://www.joeconway.com/plr/.
    3   @CMastication I was hoping for a cool way to keep data in a DB and run the normal #rstats off that. Maybe a translator from R to SQL code.
    4                     The distribution of online data usage: AT&T has recently announced it will no longer http://goo.gl/fb/eTywd #rstat
    5 @jaredlander not that I know of. Closest is sqldf package which allows #rstats and sqlite to share mem so transferring from DB to df is fast
    6 @CMastication Can #rstats run on data in a DB?Not loading it in2 a dataframe or running SQL cmds but treating the DB as if it wr a dataframe
      favorited
    1     FALSE
    2     FALSE
    3     FALSE
    4     FALSE
    5     FALSE
    6     FALSE
    

    You could turn this into a function if you intend on doing it frequently.

提交回复
热议问题