How to convert string to date format in R - r

I have a column of strings in the following format:
Wed, 6 Dec 2000 08:47:00 -0800 (PST)
How can I convert this into date format using lubridate or another package? I have done this before, but there was no -0800 (PST) at the end.
Thank you.

I was able to get a result using strptime() without even worrying about the timezone name at the end:
> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x, "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
However, if you want to remove the timezone name, you can use substr() to do this:
> strptime(substr(x, 1, nchar(x)-6), "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"

We can also use parse_date_time
library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

Related

How to convert a character timestamp into a date-time object in R

There is a timestamp variable (i.e. UTC) in my data frame which is a character / string and the date-time format is as follows:-
Fri Aug 10 04:42:47 +0000 2012
How to convert it into a date-time object in R? I tried using the following but it is giving me NAs.
data1$datetime <- as.POSIXct(as.numeric(data1$UTC),origin="1970-01-01",tz="GMT")
This works for your example. See ?strptime for the format codes.
as.POSIXct("Fri Aug 10 04:42:47 +0000 2012",format="%a %b %d %H:%M:%S %z %Y",tz="GMT")
[1] "2012-08-10 04:42:47 GMT"
You can also use parse_date_time from lubridate, which saves you the typing of spaces and % signs:
date_string = "Fri Aug 10 04:42:47 +0000 2012"
library(lubridate)
parse_date_time(date_string, "abdHMSzY", tz = "GMT")
# [1] "2012-08-10 04:42:47 GMT"

Character string with timezone convert to date

I am trying to convert a vector of dates that I read from a csv file using read.table. These were read as a vector of character strings. I am trying to convert it to a date vector using as_date.
The date vector has elements of the below type
dateString
"Wed Dec 11 00:00:00 ICT 2013"
On trying to convert using the below command,
as.Date(dateString,"%a %b %e %H:%M:%S %Z %Y")
Error in strptime(x, format, tz = "GMT") :
use of %Z for input is not supported
What would be the right format to use in strptime? or in as.Date?
Just use the anytime() function from the anytime package:
R> anytime::anytime("Wed Dec 11 00:00:00 ICT 2013")
[1] "2013-12-11 CST"
R>
There is also an utctime() variant to not impose your local time, and much. By now we also had a number of questions here so just search.
And if you want a date, it works the same way:
R> anytime::anydate("Wed Dec 11 00:00:00 ICT 2013")
[1] "2013-12-11"
R>

strptime returning NA

I am getting NA value for my date string using strptime in R.
I looked at the various answers, but it didn't work.
Here is my code
startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)
Any help would be appreciated.
"%H:%m:%S" should be "%H:%M:%S". Once you change that, you'll get an error because %Z is not valid for input.
If all the datetime strings have UTC timezone, this will work:
R> strptime("Wed May 25 01:51:32 UTC 2016", "%a %B %d %H:%M:%S UTC %Y", "UTC")
[1] "2016-05-25 01:51:32 UTC"
If not, then you can extract the year and prepend it to the string, because strptime will ignore all characters after those specified by the format string.
R> dts <- "Wed May 25 01:51:32 UTC 2016"
R> dtf <- "%Y %a %B %d %H:%M:%S"
R> strptime(paste(substring(dts, nchar(dts)-3), dts), dtf, "UTC")
[1] "2016-05-25 01:51:32 UTC"

R convert string in date format

I'm having some problems converting string vectors into date format. I have found a lot of information here and here but they haven't worked in my case (R 3.2.3).
> strptime("Fri Feb 05 14:10:10 +0000 2016",
format="%a %b %d %H:%M:%S %z %Y", tz="GMT")
[1] NA

How do I convert dates in this format to a date class in R?

I have a spreadsheet full of data with dates that look like this:
Mon Jul 16 15:20:22 +0000 2012
Is there a way to convert these to R dates (preferably PST) without using regular expression or is there no other way? I'd appreciate ideas on doing this conversion efficiently.
Sure, just use strptime() to parse time from strings:
R> strptime("Mon Jul 16 15:20:22 +0000 2012",
+ format="%a %b %d %H:%M:%S %z %Y")
[1] "2012-07-16 10:20:22 CDT"
R>
which uses my local timezone (CDT). If yours is Pacific, you can set it explicitly as in
R> strptime("Mon Jul 16 15:20:22 +0000 2012",
+ format="%a %b %d %H:%M:%S %z %Y", tz="America/Los_Angeles")
[1] "2012-07-16 08:20:22 PDT"
R>
which looks right with a 7 hour delta to UTC.
There's nearly a verbatim example of how to do this in the Examples section of ?strptime:
# ?strptime example:
## An RFC 822 header (Eastern Canada, during DST)
strptime("Tue, 23 Mar 2010 14:36:38 -0400", "%a, %d %b %Y %H:%M:%S %z")
# your data...
strptime("Mon Jul 16 15:20:22 +0000 2012", "%a %b %d %H:%M:%S %z %Y")
This can also be done with lubridate package in tidyverse
library(lubridate)
parse_date_time("Mon Jul 16 15:20:22 +0000 2012", orders = "amdHMSzY")
which is what I prefer.

Resources