Formatting dates with R - r

I need to format a date variable with R. I get the current date with a simple date().
today <- date()
today
> "Mon Oct 10 1:00 2016"
I need to format this 'today' variable into a string with a specific format. below is an example of what the string should look like.
string <- ā€10/10/2016 1:00 PM EDT"
string
> ā€10/10/2016 1:00 PM EDT"
So the question is how do you format a character string that looks like "Mon Oct 10 1:00 2016" into ā€10/10/2016 1:00 PM EDT".
I've tried working with strptime() and as.Date() functions but cannot figure out how to convert this string into a formatted date. Thanks for any help.

strftime(strptime(date(),
format = "%a %b %d %H:%M:%S %Y"),
format = "%m/%d/%Y %I:%M %p %Z")
See ??strptime.

Related

Extract time stamps from string and convert to R POSIXct object

Currently, my dataset has a time variable (factor) in the following format:
weekday month day hour min seconds +0000 year
I don't know what the "+0000" field is but all observations have this. For example:
"Tues Feb 02 11:05:21 +0000 2018"
"Mon Jun 12 06:21:50 +0000 2017"
"Wed Aug 01 11:24:08 +0000 2018"
I want to convert these values to POSIXlt or POSIXct objects(year-month-day hour:min:sec) and make them numeric. Currently, using as.numeric(as.character(time-variable)) outputs incorrect values.
Thank you for the great responses! I really appreciate a lot.
Not sure how to reproduce the transition from factor to char, but starting from that this code should work:
t <- unlist(strsplit(as.character("Tues Feb 02 11:05:21 +0000 2018")," "))
strptime(paste(t[6],t[2],t[3], t[4]),format='%Y %b %d %H:%M:%S')
PS: More on date formats and conversion: https://www.stat.berkeley.edu/~s133/dates.html
For this problem you can get by without using lubridate. First, to extract individual dates we can use regmatches and gregexpr:
date_char <- 'Tue Feb 02 11:05:21 +0000 2018 Mon Jun 12 06:21:50 +0000 2017'
ptrn <- '([[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}\\:[[:digit:]]{2}\\:[[:digit:]]{2} \\+[[:digit:]]{4} [[:digit:]]{4})'
date_vec <- unlist( regmatches(date_char, gregexpr(ptrn, date_char)))
> date_vec
[1] "Tue Feb 02 11:05:21 +0000 2018" "Mon Jun 12 06:21:50 +0000 2017"
You can learn more about regular expressions here.
In the above example +0000 field is the UTC offset in hours e.g. it would be -0500 for EST timezone. To convert to R date-time object:
> as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC')
[1] "2018-02-02 11:05:21 UTC" "2017-06-12 06:21:50 UTC"
which is the desired output. The formats can be found here or you can use lubridate::guess_formats(). If you don't specify the tz, you'll get the output in your system's time zone (e.g. for me that would be EST). Since the offset is specified in the format, R correctly carries out the conversion.
To get numeric values, the following works:
> as.numeric(as.POSIXct(date_vec, format = '%a %b %d %H:%M:%S %z %Y', tz = 'UTC'))
[1] 1517569521 1497248510
Note: this is based on uniform string structure. In the OP there was Tues instead of Tue which wouldn't work. The above example is based on the three-letter abbreviation which is the standard reporting format.
If however, your data is a mix of different formats, you'd have to extract individual time strings (customized regexes, of course), then use lubridate::guess_formats() to get the formats and then use those to carry out the conversion.
Hope this is helpful!!

Convert date strings with R

Iā€™m working with date strings in R. Essentially, I have three different strings that represent date variables. I have these weird date strings from scraping data on the web.
Is it possible to convert these three different date strings into a universal format that makes it easier to perform logic on them with basic R code? Here are what the strings look like. Any help is greatly appreciated.
1. "Wed, Feb 7, 2017 7:30 pm"
2. "Wed Feb 7 08:00:04 2017"
3. "2017-02-7 13:06:14 PST" # Sys.time()
UPDATE: I now have a better understanding of as.POSIXct now, but I still don't understand why this doesn't work ?
as.POSIXct('02/15/2017, 10:00 PM', format = "%M/%D/%Y, %H:%M %r")
While your specific question has already been answered in comments.
I would like to leave this as a general reference for other people who might have similar problems and can come across this question.
So, as you have this in d.b's comment, your data time string have been parsed via command:
as.POSIXct("Wed, Feb 7, 2017 7:30 pm", format = "%A, %b %d,%Y %H:%M")
The difference between your first and the second case was in the format.
So, this is a general guidance on the format:
%a Abbreviated weekday
%A Full weekday
%b Abbreviated month
%B Full month
%c Locale-specific date and time
%d Decimal date
%H Decimal hours (24 hour)
%I Decimal hours (12 hour)
%j Decimal day of the year
%m Decimal month
%M Decimal minute
%p Locale-specific AM/PM
%S Decimal second
%U Decimal week of the year (starting on Sunday)
%w Decimal Weekday (0=Sunday)
%W Decimal week of the year (starting on Monday)
%x Locale-specific Date
%X Locale-specific Time
%y 2-digit year
%Y 4-digit year
%z Offset from GMT
%Z Time zone (character)
This is also useful if you want to do the conversions between different formats:
x <- as.POSIXct( "2017-01-15")
format(x, "%a")
[1] "Sun"
format(x, "Week of the year: %W")
[1] "Week of the year: 02"
source: https://www.stat.berkeley.edu/~s133/dates.html
as.POSIXct("Wed, Feb 7, 2017 7:30 pm", format = "%A, %b %d,%Y %H:%M", tz="PST8PDT")
as.POSIXct("Wed Feb 7 08:00:04 2017", format = "%A %b %d %H:%M:%S %Y",tz="PST8PDT")
as.POSIXct("2017-02-7 13:06:14 PST", format = "%Y-%m-%d %H:%M:%S",tz="PST8PDT")

How to convert string to date format in 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"

Convert this Character string to Date in r

I have the data in character format as " Mar 26, 2015 7:46:22 PM CDT " I have convert this into a Date format and also fetch the month, year and day separately.
Also is it possible to conevert it into a integer format.
Please advice.
You have to specify the format of the input string. Try:
as.POSIXct("Mar 26, 2015 7:46:22 PM CDT", format="%b %d, %Y %I:%M:%S %p")

converting utc format to required format using python

I am trying to convert UTC format date format to required format using python. Simply I have the datetime in the format(Fri Dec 07 19:06:06 +0000 2012), I need to convert this into my format(2012-12-07 19:06:06:546 +0000)
Code:
created_at = "Fri Dec 07 19:06:06 +0000 2012"
d = datetime.strptime(created_at, '%a %b %d %H:%M:%S %z %Y')
date_object = d.strftime('%y-%m-%d %H:%M:%S')
result:
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'
The below link from python bugs says its fixed but i didn understand what is fixed and in which version i can use %z
http://bugs.python.org/issue6641
I am not able to use %z . Is there any other way to handle this??
Do it like that:
from datetime import datetime
TIMESTAMP = datetime.utcnow().strftime('%d/%m/%Y %H:%M:%S')

Resources