gpt4 book ai didi

r - 如何将 searchTwitter 结果(来自库(twitteR))转换为 data.frame?

转载 作者:行者123 更新时间:2023-12-02 01:39:04 24 4
gpt4 key购买 nike

我正在将 twitter 搜索结果保存到数据库 (SQL Server) 中,但当我从 twitter 中提取搜索结果时出现错误。

如果我执行:

library(twitteR)
puppy <- as.data.frame(searchTwitter("puppy", session=getCurlHandle(),num=100))

我收到错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class structure("status", package = "twitteR") into a data.frame

这很重要,因为为了使用 RODBC 将其添加到使用 sqlSave 的表中,它需要是一个 data.frame。至少这是我收到的错误消息:

Error in sqlSave(localSQLServer, puppy, tablename = "puppy_staging",  : 
should be a data frame

那么有人对如何将列表强制到 data.frame 或如何通过 RODBC 加载列表有任何建议吗?

我的最终目标是拥有一个反射(reflect) searchTwitter 返回值结构的表。这是我尝试检索和加载的示例:

library(twitteR)
puppy <- searchTwitter("puppy", session=getCurlHandle(),num=2)
str(puppy)

List of 2
$ :Formal class 'status' [package "twitteR"] with 10 slots
.. ..@ text : chr "beautifull and kc reg Beagle Mix for rehomes: This little puppy is looking for a new loving family wh... http://bit.ly/9stN7V "| __truncated__
.. ..@ favorited : logi FALSE
.. ..@ replyToSN : chr(0)
.. ..@ created : chr "Wed, 16 Jun 2010 19:04:03 +0000"
.. ..@ truncated : logi FALSE
.. ..@ replyToSID : num(0)
.. ..@ id : num 1.63e+10
.. ..@ replyToUID : num(0)
.. ..@ statusSource: chr "&lt;a href=&quot;http://twitterfeed.com&quot; rel=&quot;nofollow&quot;&gt;twitterfeed&lt;/a&gt;"
.. ..@ screenName : chr "puppy_ads"
$ :Formal class 'status' [package "twitteR"] with 10 slots
.. ..@ text : chr "the cutest puppy followed me on my walk, my grandma won't let me keep it. taking it to the pound sadface"
.. ..@ favorited : logi FALSE
.. ..@ replyToSN : chr(0)
.. ..@ created : chr "Wed, 16 Jun 2010 19:04:01 +0000"
.. ..@ truncated : logi FALSE
.. ..@ replyToSID : num(0)
.. ..@ id : num 1.63e+10
.. ..@ replyToUID : num(0)
.. ..@ statusSource: chr "&lt;a href=&quot;http://blackberry.com/twitter&quot; rel=&quot;nofollow&quot;&gt;Twitter for BlackBerry®&lt;/a&gt;"
.. ..@ screenName : chr "iamsweaters"

所以我认为 puppy 的 data.frame 应该具有如下列名称:

- text
- favorited
- replytoSN
- created
- truncated
- replytoSID
- id
- replytoUID
- statusSource
- screenName

最佳答案

我使用从 http://blog.ouseful.info/2011/11/09/getting-started-with-twitter-analysis-in-r/ 找到的代码不久前:

#get data
tws<-searchTwitter('#keyword',n=10)

#make data frame
df <- do.call("rbind", lapply(tws, as.data.frame))

#write to csv file (or your RODBC code)
write.csv(df,file="twitterList.csv")

关于r - 如何将 searchTwitter 结果(来自库(twitteR))转换为 data.frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3056146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com