Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Twitter Timestamp in R

Tags:

datetime

r

I am new to R and am terrible with handling dates. The following date is returned from a query to the Twitter search API and is stored as a character string in a my dataframe.

"Fri, 14 Jan 2011 03:01:22 +0000"

How can I convert this to a date and change the timezone to be Eastern Standard time?

I figure this is probably straight forward, but I dabbled with strptime and got nowhere.

Any help will be greatly appreciated!

like image 704
Btibert3 Avatar asked Jan 14 '11 22:01

Btibert3


3 Answers

This works for me (I'm in the UK):

> ( str <- "Fri, 14 Jan 2011 03:01:22 +0000" )
[1] "Fri, 14 Jan 2011 03:01:22 +0000"

> ( str <- strptime(str, "%a, %d %b %Y %H:%M:%S %z", tz = "GMT") )
[1] "2011-01-14 03:01:22 GMT"

> ( dt.gmt <- as.POSIXct(str, tz = "GMT") )
[1] "2011-01-14 03:01:22 GMT"

> format(dt.gmt, tz = "EST", usetz = TRUE)
[1] "2011-01-13 22:01:22 EST"

Dates/times confuse me a lot, so I'm hoping the above works for you, even if you're in a different timezone from GMT, but I can't be sure!

Hope it helps a little at least, Tony

like image 97
Tony Breyal Avatar answered Oct 19 '22 07:10

Tony Breyal


From the help(strptime):

> Sys.setlocale("LC_TIME", "C")
[1] "C"
> strptime("Tue, 23 Mar 2010 14:36:38 -0400",
+          "%a, %d %b %Y %H:%M:%S %z",
+          tz="GMT")
[1] "2010-03-23 18:36:38 GMT"

Be careful about the locale: if you don't reset it to C, the function will try to parse weekday and month abbreviations as localized.

like image 43
ulidtko Avatar answered Oct 19 '22 07:10

ulidtko


I strongly recommend that you take a look at Jeff Gentry's twitteR package on CRAN. Among other niceties, it parses date strings for you:

> library(twitteR)
> tweets = searchTwitter('#rstats')
> length(tweets)
[1] 25
> tweet = tweets[[1]]
> str(tweet)
Formal class 'status' [package "twitteR"] with 10 slots
  ..@ text        : chr "The Joy of Sweave \023 A Beginner\031s Guide to Reproducible Research with Sweave: Just& http://goo.gl/fb/APmCb #rstats"
  ..@ favorited   : logi FALSE
  ..@ replyToSN   : chr(0) 
  ..@ created     : POSIXct[1:1], format: "2011-01-18 04:48:05"
  ..@ truncated   : logi FALSE
  ..@ replyToSID  : num(0) 
  ..@ id          : num 2.72e+16
  ..@ replyToUID  : num(0) 
  ..@ statusSource: chr "&lt;a href=&quot;http://www.google.com/support/youtube/bin/answer.py?hl=en&amp;answer=164577&quot; rel=&quot;nofollow&quot;&gt;"| __truncated__
  ..@ screenName  : chr "Rbloggers"
like image 1
Jeffrey Breen Avatar answered Oct 19 '22 07:10

Jeffrey Breen