datetimer

Convert Twitter Timestamp in 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!


Solution

  • 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