Convert timestamp to R to R

I am new to R and have a terrible attitude towards processing dates. The next date is returned from the request in the Twitter search API and stored as a character string in my data core.

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

How can I convert this value to a date and change the time zone to Eastern Standard Time?

I suppose this is probably straightforward, but I mumbled with strptime and didn't get anywhere.

Any help would be greatly appreciated!

+5
source share
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"

/ , , , GMT, !

, ,

+9

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"

: reset C, .

+6

I highly recommend you take a look at the Jeff Gentry twitteR CRAN package. Among other subtleties, 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"
+1
source

All Articles