将Twitter Stream JSON转换为R数据框 [英] Converting Twitter Stream JSON to R dataframe

查看:133
本文介绍了将Twitter Stream JSON转换为R数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON格式的文本文件,该文件是使用此

I have a text file in JSON format that I downloaded using code from this link. It looks something like this:

"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"'ello miss...?\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954853199872\",\"favorited\":false,\"geo\":null,\"source\":\"\u003Ca href=\\"http:\/\/www.tweetdeck.com\\" rel=\\"nofollow\\"\u003ETweetDeck\u003C\/a\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"en\",\"friends_count\":145,\"profile_sidebar_border_color\":\"181A1E\",\"location\":\"Chile\",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/images\/themes\/theme9\/bg.gif\",\"id_str\":\"76862348\",\"listed_count\":5,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/profile_images\/1547896482\/egotuiter_normal.png\",\"description\":\"Geek. Amante de la m\u00fasica. Ateo. Nortino. Estudio Derecho en la U. de Chile. Nadie lee mi blog. Cobreloino. Afortunadamente ya no uso lentes :D\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"666666\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/images\/themes\/theme9\/bg.gif\",\"followers_count\":181,\"is_translator\":false,\"time_zone\":\"Buenos Aires\",\"profile_link_color\":\"2FC2EF\",\"protected\":false,\"created_at\":\"Thu Sep 24 05:11:20 +0000 2009\",\"profile_background_color\":\"1A1B1F\",\"name\":\"Camilo Rojas\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":36450,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":\"http:\/\/unnombreingenioso.blogspot.com\",\"profile_image_url\":\"http:\/\/a0.twimg.com\/profile_images\/1547896482\/egotuiter_normal.png\",\"screen_name\":\"rojas_altazor\",\"id\":76862348,\"verified\":false,\"utc_offset\":-10800,\"favourites_count\":42,\"profile_sidebar_fill_color\":\"252429\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954853199872}
"
"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"VALENTINA CORTANTE!\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954861580288\",\"favorited\":false,\"geo\":null,\"source\":\"web\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"es\",\"friends_count\":251,\"profile_sidebar_border_color\":\"CC3366\",\"location\":\"\",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/images\/themes\/theme11\/bg.gif\",\"id_str\":\"292675295\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/profile_images\/2494400748\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"description\":\"Soy hermosaaaa y valen me ama\u2665\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"362720\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/images\/themes\/theme11\/bg.gif\",\"followers_count\":399,\"is_translator\":false,\"time_zone\":\"Santiago\",\"profile_link_color\":\"B40B43\",\"protected\":false,\"created_at\":\"Wed May 04 01:35:06 +0000 2011\",\"profile_background_color\":\"FF6699\",\"name\":\"Valen me amodora\u2020\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":2643,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":true,\"url\":null,\"profile_image_url\":\"http:\/\/a0.twimg.com\/profile_images\/2494400748\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"screen_name\":\"CamiCelie\",\"id\":292675295,\"verified\":false,\"utc_offset\":-14400,\"favourites_count\":2,\"profile_sidebar_fill_color\":\"E5507E\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954861580288}
"

我很好奇如何将单个片段提取到R数据帧中.例如,理想情况下,它将转到这样的数据帧:

I'm curious how to extract individual pieces to an R data frame. For instance this would ideally go to a data frame like this:

screen_name        text
rojas_altazor       'ello miss...?
CamiCelie           VALENTINA CORTANTE!
...                  ...

这是我首次尝试使用JSON文件,因此任何建议将不胜感激.

This is my first foray into using JSON files so any suggestions would be appreciated.

推荐答案

twit<-c("{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"'ello miss...?\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954853199872\",\"favorited\":false,\"geo\":null,\"source\":\"\\u003Ca href=\\\"http:\\/\\/www.tweetdeck.com\\\" rel=\\\"nofollow\\\"\\u003ETweetDeck\\u003C\\/a\\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"en\",\"friends_count\":145,\"profile_sidebar_border_color\":\"181A1E\",\"location\":\"Chile\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme9\\/bg.gif\",\"id_str\":\"76862348\",\"listed_count\":5,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1547896482\\/egotuiter_normal.png\",\"description\":\"Geek. Amante de la m\\u00fasica. Ateo. Nortino. Estudio Derecho en la U. de Chile. Nadie lee mi blog. Cobreloino. Afortunadamente ya no uso lentes :D\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"666666\",\"default_profile\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme9\\/bg.gif\",\"followers_count\":181,\"is_translator\":false,\"time_zone\":\"Buenos Aires\",\"profile_link_color\":\"2FC2EF\",\"protected\":false,\"created_at\":\"Thu Sep 24 05:11:20 +0000 2009\",\"profile_background_color\":\"1A1B1F\",\"name\":\"Camilo Rojas\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":36450,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":\"http:\\/\\/unnombreingenioso.blogspot.com\",\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/1547896482\\/egotuiter_normal.png\",\"screen_name\":\"rojas_altazor\",\"id\":76862348,\"verified\":false,\"utc_offset\":-10800,\"favourites_count\":42,\"profile_sidebar_fill_color\":\"252429\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954853199872}", 
"", "{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"VALENTINA CORTANTE!\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954861580288\",\"favorited\":false,\"geo\":null,\"source\":\"web\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"es\",\"friends_count\":251,\"profile_sidebar_border_color\":\"CC3366\",\"location\":\"\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme11\\/bg.gif\",\"id_str\":\"292675295\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2494400748\\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"description\":\"Soy hermosaaaa y valen me ama\\u2665\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"362720\",\"default_profile\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme11\\/bg.gif\",\"followers_count\":399,\"is_translator\":false,\"time_zone\":\"Santiago\",\"profile_link_color\":\"B40B43\",\"protected\":false,\"created_at\":\"Wed May 04 01:35:06 +0000 2011\",\"profile_background_color\":\"FF6699\",\"name\":\"Valen me amodora\\u2020\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":2643,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":true,\"url\":null,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2494400748\\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"screen_name\":\"CamiCelie\",\"id\":292675295,\"verified\":false,\"utc_offset\":-14400,\"favourites_count\":2,\"profile_sidebar_fill_color\":\"E5507E\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954861580288}", 
"")


library(RJSONIO)

twit1<-fromJSON(twit[1])

> twit1$user$screen_name
[1] "rojas_altazor"

> twit1$text
[1] "'ello miss...?"

> twit2<-fromJSON(twit[3])
> twit2$user$screen_name
[1] "CamiCelie"
> twit2$text
[1] "VALENTINA CORTANTE!"

这篇关于将Twitter Stream JSON转换为R数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆