Skip to content

Instantly share code, notes, and snippets.

@alfcrisci
Last active August 29, 2015 14:17
Show Gist options
  • Save alfcrisci/d07934eca683699a4988 to your computer and use it in GitHub Desktop.
Save alfcrisci/d07934eca683699a4988 to your computer and use it in GitHub Desktop.
to read in Mearelli style json tweet object
ls_files=Sys.glob("*.json")
tweetdf(jsonfile)
tweetdf=function(jsonfile) {
require(jsonlite)
ex=jsonlite::fromJSON(jsonfile)
timetw=as.POSIXct(as.character(ex$created_at), format="%a %b %d %H:%M:%S %z %Y")
id_str=as.character(ex$id)
from_user=ex$user$screen_name;
status_url=as.character(ex$user$status_url);
profile_image_url=as.character(ex$user$profile_image_url)
text=as.character(ex$text)
created_at=as.character(ex$created_at)
from_user_id_str=ifelse(is.null(ex$user$id_str)," ",as.character(ex$user$id_str))
in_reply_to_status_id_str=ifelse(is.null(ex$in_reply_to_status_id)," ",as.character(ex$in_reply_to_status_id))
in_reply_to_user_id_str=ifelse(is.null(ex$in_reply_to_user_id)," ",as.character(ex$in_reply_to_user_id))
in_reply_to_screen_name=ifelse(is.null(ex$in_reply_to_screen_name)," ",as.character(ex$in_reply_to_screen_name))
user_followers_count=ifelse(is.null(ex$user$user_followers_count),"0",as.character(ex$user$user_followers_count))
user_friends_count=ifelse(is.null(ex$user$user_friends_count),"0",as.character(ex$user$user_followers_count))
retweet_count=ifelse(is.null(as.character(ex$retweet_count)),"0",as.character(ex$retweet_count))
favorited=ifelse(is.null(as.character(ex$favorited)),"0",as.character(ex$favorited))
extended_entities_str=ifelse(is.null(ex$extended_entities)," ",as.character(jsonlite::toJSON(ex$extended_entities)))
entities_str=ifelse(is.null(ex$entities)," ",as.character(jsonlite::toJSON(ex$entities)))
geo=ifelse(is.null(ex$geo)," ",as.character(jsonlite::toJSON(ex$geo)))
place=ifelse(is.null(ex$place)," ",as.character(jsonlite::toJSON(ex$place)))
geo_coordinates=ifelse(is.null(ex$coordinates)," ",as.character(jsonlite::toJSON(ex$coordinates)))
lang=ifelse(is.null(ex$lang)," ",as.character(ex$lang))
user_lang=ifelse(is.null(ex$user$lang)," ",as.character(ex$user$lang));
place=ifelse(is.null(ex$place)," ",as.character(jsonlite::toJSON(ex$place)))
sourcer=ifelse(is.null(ex$source)," ",as.character(ex$source))
status_url=paste0("https://twitter.com/",as.character(ex$user$screen_name),"/statuses/",id_str)
return(data.frame(id_str=id_str,
from_user=from_user,
text=text,
created_at=created_at,
time=timetw,
geo_coordinates=geo_coordinates,
user_lang=user_lang,
in_reply_to_user_id_str=in_reply_to_user_id_str,
in_reply_to_screen_name=in_reply_to_screen_name,
from_user_id_str=from_user_id_str,
in_reply_to_status_id_str=in_reply_to_status_id_str,
source=sourcer,
profile_image_url=profile_image_url,
user_followers_count=user_friends_count,
user_friends_count=user_friends_count,
status_url=status_url,
entities_str=entities_str,
extended_entities_str=extended_entities_str,
retweet_count=retweet_count,
favorited=favorited,
geo=geo,
lang=lang,
place=place
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment