Converting time format in R - r

One of my observation of time data is "Wed Oct 31 13:42:46 2007".
I want to convert to the format as "2007-10-31 13:42:46" (remove Wed).
How I can convert it?

As #thelatemail said
x <- "Wed Oct 31 13:42:46 2007"
x <- as.POSIXct(x, format = "%a %b %d %T %Y", tz = "GMT")
# %a day of week, %b month of year, %d day of month as decimal, %T H:M:S, %Y year with century
x
[1] "2007-10-31 13:42:46 GMT"

Related

in R, modify date from aug 07, 2020 to 08,07,2020, also how to remove time zone

[Fri Aug 07, 2020 05:12 UTC]
I have this date format in a column, how to modify it to be 08, 07,2020 05:12
also, how to remove UTC from all columns
Check ?strptime for various format options. First convert the data to POSIXct, you can then use format to get it any format that you want.
x <- 'Fri Aug 07, 2020 05:12 UTC'
x1 <- as.POSIXct(x, format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC')
x1
#[1] "2020-08-07 05:12:00 UTC"
format(x1, '%m,%d,%Y %H:%M')
#[1] "08,07,2020 05:12"
If we want to apply this for multiple columns we can use lapply. For example for first 4000 columns where your dataframe is called df we can do :
cols <- 1:4000
df[cols] <- lapply(df[cols], function(x) format(as.POSIXct(x,
format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC'), '%m,%d,%Y %H:%M'))

Convert char to POSIXct

this is my date to convert:
"16:00:00 CT 08 Apr 2018"
and this is my try:
x <- "16:00:00 CT 08 Apr 2018"
Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
as.POSIXct(x, format = '%H:%M:%S %A %d %b %Y')
and it returns NA
We can use the CT as such in the format
as.POSIXct(x, format = '%H:%M:%S CT %d %b %Y')
Remove string CT and go further:
as.POSIXct(paste(unlist(strsplit(x," CT ")),collapse = ""),format='%H:%M:%S %d %b %Y')
[1] "2018-04-08 16:00:00 GMT"

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")

Adjust data time zone in R

For some reason I can't adjust the time zone by as.POSIXlt.
time <- "Wed Jun 22 01:53:56 +0000 2016"
t <- strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
t
[1] "2016-06-21 21:53:56"
Can't change the time zone
as.POSIXlt(t, "EST")
[1] "2016-06-21 21:53:56"
as.POSIXlt(t, "Australia/Darwin")
[1] "2016-06-21 21:53:56"
Can change the time zone for Sys.time()
as.POSIXlt(Sys.time(), "EST")
[1] "2016-09-26 01:47:22 EST"
as.POSIXlt(Sys.time(), "Australia/Darwin")
[1] "2016-09-26 16:19:48 ACST"
How to solve it?
Try this:
time <- "Wed Jun 22 01:53:56 +0000 2016"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
#[1] "2016-06-22 07:23:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="EST")
#[1] "2016-06-21 20:53:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="Australia/Darwin")
#[1] "2016-06-22 11:23:56"
strptime returns a POSIXlt object. Calling as.POSIXlt on t just returns t. There is no as.POSIXlt.POSIXlt method, so as.POSIXlt.default is dispatched. And you can see the first if statement checks if x inherits the POSIXlt class, and returns x if that's true.
str(t)
# POSIXlt[1:1], format: "2016-06-21 20:53:56"
print(as.POSIXlt.default)
# function (x, tz = "", ...)
# {
# if (inherits(x, "POSIXlt"))
# return(x)
# if (is.logical(x) && all(is.na(x)))
# return(as.POSIXlt(as.POSIXct.default(x), tz = tz))
# stop(gettextf("do not know how to convert '%s' to class %s",
# deparse(substitute(x)), dQuote("POSIXlt")), domain = NA)
# }
# <bytecode: 0x2d6aa18>
# <environment: namespace:base>
You either need to use as.POSIXct instead of strptime and specify the timezone you want, then convert to POSIXlt:
ct <- as.POSIXct(time, tz = "Australia/Darwin", format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(ct)
Or use strptime and convert t to POSIXct and then back to POSIXlt:
t <- strptime(time, format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(as.POSIXct(t, tz = "Australia/Darwin"))

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"

Resources