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!
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
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.
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 "<a href="http://www.google.com/support/youtube/bin/answer.py?hl=en&answer=164577" rel="nofollow">"| __truncated__
..@ screenName : chr "Rbloggers"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With