0

I already read a lot of questions like that, but I guess I'm facing a different character vector here, so none of the answers solved my problem.

I have a character vector like the follwoing

"Fri Sep 18 17:01:33 +0000 2015"

I'd like to transform into a Date vector like

2015-09-18 17:01:33

Then I could make plots with these.

If that helps, this data come from Twitter API using streamR package.

Thank you.

EDIT: All the efforts of my colleagues was resulting in NA only in my computer, because my system language is Brazilian Portuguese. Found how to solve the problem here:

strptime, as.POSIXct and as.Date return unexpected NA

Community
  • 1
  • 1
  • First I tried `gsub` to eliminate the '+0000' part, but It didn't work `> teste` `[1] "Fri Sep 18 17:01:33 +0000 2015"` `> gsub('+0000', ' ', teste)` `[1] "Fri Sep 18 17:01:33 + 2015"` then I tried strptime but returned NA `> df$created_at[1]` `[1] "Fri Sep 18 17:01:33 +0000 2015"` `> teste <- strptime(df$created_at, "%a %b %d %H:%M:%S")` `> teste[1]` `[1] NA` – Luiz Felipe Freitas Sep 18 '15 at 21:32

2 Answers2

1

You do not need to eliminate the +0000, it is the offset expressed in minutes from the UTC and some times (not this one which is 0000) it might be very useful and convert the data in the proper way.

Here is a working example with strptime:

x <- "Fri Sep 18 17:01:33 +0000 2015"
strptime(x, "%a %b %d %H:%M:%S %z %Y", tz = "UTC")
[1] "2015-09-18 17:01:33 UTC"
SabDeM
  • 7,050
  • 2
  • 25
  • 38
1

You are doing the gsub wrong. You should escape the + symbol so that your pattern is '\\+0000'. Combined:

    > x <- "Fri Sep 18 17:01:33 +0000 2015"
    > gsub('\\+0000', '', x)
    [1] "Fri Sep 18 17:01:33  2015"
    > x <- gsub('\\+0000', '', x)
    > strptime(x, format = '%a %b %d %H:%M:%S  %Y')
    [1] "2015-09-18 17:01:33 PDT"

That should do it.

user3274289
  • 2,426
  • 3
  • 16
  • 14