Convert Twitter Timestamp in R

后端 未结 3 1117

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.<

相关标签:
3条回答
  • 2021-02-09 19:40

    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

    0 讨论(0)
  • 2021-02-09 19:49

    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.

    0 讨论(0)
  • 2021-02-09 19:55

    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"
    
    0 讨论(0)
提交回复
热议问题