2016-02-26 77 views
0

使用SearchTwitter时,我转换为数据框,然后导出为JSON。但是,所有文本都在一行中,等等(下面的示例)。我需要分开,以便每条推文都是自己的。将R JSON Twitter数据转换为列表

phish <- searchTwitteR('phish', n = 5, lang = 'en') 
phishdf <- do.call("rbind", lapply(phish, as.data.frame)) 
exportJson <-toJSON(phishdf) 
write(exportJson, file = "phishdf.json") 
json_phishdf <- fromJSON(file="phishdf.json") 

我试图转换为列表,并想知道是否可能转换为数据帧是一个错误。

然而,对于一个名单,我想:

newlist['text']=phish[[1]]$getText() 

但是,这只是给我的第一鸣叫的文本。有没有办法遍历整个数据集,也许在for循环?

{"text":["@ilazer @abbijacobson I do feel compelled to say that I phind phish awphul... sorry, Abbi!","@phish This on-sale was an embarrassment. Something needs to change.","FS: Have 2 Tix To Phish In Chula Vista @Phish #facevaluetickets #phish #facevalue GO: https://t.co/dFdrpyaotp","RT @WKUPhiDelt: Come unwind from a busy week of class and kick off the weekend with a Phish Fry! 4:30-7:30 at the Phi Delt house. Cost is $\u2026","RT @phish: Tickets for Phish's July 15 &amp; 16 shows at The Gorge go on sale in fifteen minutes at 1PM ET: https://t.co/tEKLNjI5u7 https://t.c\u2026"], 

"favorited":[false,false,false,false,false], 

"favoriteCount":[0,0,0,0,0], 

"replyToSN":["rAlexandria","phish","NA","NA","NA"], 

"created":[1456521159,1456521114,1456521022,1456521016,1456520988], 

"truncated":[false,false,false,false,false], 

"replyToSID":["703326502629277696","703304948990222337","NA","NA","NA"], 

"id":["703326837720662016","703326646074343424","703326261045829632","703326236722991105","703326119328686080"], 

"replyToUID":["26152867","14503997","NA","NA","NA"],"statusSource":["<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Mobile Web (M5)</a>","<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>","<a href=\"http://cashortrade.org\" rel=\"nofollow\">CashorTrade - Face Value Tickets</a>","<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>","<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>"], 

"screenName":["rAlexandria","adamgelvan","CashorTrade","Kyle_Smith1087","timogrennell"], 

"retweetCount":[0,0,0,2,5], 

"isRetweet":[false,false,false,true,true], 

"retweeted":[false,false,false,false,false], 

"longitude":["NA","NA","NA","NA","NA"], 

"latitude":["NA","NA","NA","NA","NA"]} 
+0

使用'dput'张贴在其上请求建议可再生的数据对象。发布库调用所有需要函数的包。 –

回答

1

我跟着你的代码,没有你描述的问题。您是否在使用和library(jsonlite)

下面是代码,并且它的屏幕截图的工作

library(twitteR) 
library(jsonlite) 

phish <- searchTwitteR('phish', n = 5, lang = 'en') 
phishdf <- do.call("rbind", lapply(phish, as.data.frame)) 

exportJson <-toJSON(phishdf) 

write(exportJson, file = "./../phishdf.json") 

## note the `txt` argument, as opposed to `file` used in the question 
json_phishdf <- fromJSON(txt="./../phishdf.json") 

enter image description here

+0

真棒。我没有使用jsonlite,想知道这是否是问题的一部分。无论是或更改文件到txt工作。谢谢! – user6754289