Reputation: 627
I am trying to convert the created_at string but it returns NA
as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] NA
Any idea what's going wrong, seems fairly straightforward!
Upvotes: 0
Views: 1589
Reputation: 315
a solution that doesn't involve changing your locale
library(dplyr)
library(magrittr)
twitter_to_POSIXct <- function(x, timezone = Sys.timezone()){
x %>%
strsplit("\\s+") %>%
unlist %>%
t %>%
as.data.frame(stringsAsFactors = FALSE) %>%
set_colnames(c("week_day", "month_abb",
"day", "hour", "tz",
"year")) %>%
mutate(month_num = which(month.abb %in% month_abb)) %>%
mutate(date_str = paste0(year, "-", month_num, "-", day, " ",
hour)) %>%
mutate(date = format(as.POSIXct(date_str, tz = tz),
tz = timezone)) %>%
pull(date)
}
twitter_to_POSIXct("Tue Jun 07 23:27:12 +0000 2016")
Upvotes: 0
Reputation: 70643
Conversion of dates depends on your locale. For me, this is Slovene, so your case doesn't work.
> as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] NA
However, if I change the date to Slovene (Tor = torek = Tuesday)
> as.POSIXct("Tor Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] "2016-06-07 23:27:12 GMT"
In short, change your locale to English and you're set.
> Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
> as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] "2016-06-07 23:27:12 GMT"
Upvotes: 3