Problems converting variable into proper datetime format in R - r

My data, DATA, has a variable, TIME, for which the values print out in this format: "11/14/2006 20:10". For TIME, its mode is numeric and its class is a factor.
I need to convert TIME to a proper date/time variable (DTIME) and add the new DTIME to DATA as date.time. I was told I may have to coerce the time values so that they follow the h:m:s format...Think character string manipulation. Below is my code:
library("chron")
VAR=c(as.character(DATA$TIME))
DT<-t(as.data.frame(strsplit(VAR," ")))
DT[1:3,]
row.names(DT)<-NULL
DT[1:3,]
DTIME<-chron(dates=DT[,1],times=DT[,2],
format=c("m/d/y","h:m"))
But once I run the last line of code, I get the following error message:
Error in convert.times(times., format = format[[2]]) :
format h:m may be incorrect
In addition: Warning message:
In is.na(out$s) : is.na() applied to non-(list or vector) of type 'NULL'
I don't understand what this means, much less how to fix it.

Its not clear from the question exactly what you have -- in such cases its best to show the output of dput applied to your variable -- but assuming you can convert it to character format using as.character or format then its just a matter of using as.chron :
> library(chron)
> TIME <- "11/14/2006 20:10"
> as.chron(TIME, "%m/%d/%Y %H:%M")
[1] (11/14/06 20:10:00)

Use lubridate - it's a fantastic library that will save you much effort.
library(lubridate)
x <- "11/14/2006 20:10"
> mdy_hm(x)
[1] "2006-11-14 20:10:00 UTC"
All lubridate's time conversion function follow a very similar pattern: e.g. "2013-04-01" can be parsed with ymd, etc.

You can use as.POSIXct to convert string into time.
TIME <- "11/14/2006 20:10"
as.POSIXct(TIME, format="%m/%d/%Y %H:%M", tz='GMT')
## [1] "2006-11-14 20:10:00 GMT"

Related

Problems with parse_date_time converting a character vector

I have an imported CSV in R which contains a column of dates and times - this is imported into R as character. The format is "30/03/2020 08:59". I want to convert these strings into a format that allows me to work on them. For simplicity I have made a dataframe which has a single column of these dates (854) in this format.
I'm trying to use the parse_date_time function from lubridate.
It works fine when I reference a single value, e.g.
b=parse_date_time(consults_dates[3,1],orders="dmy HM")
gives b=2020-03-30 09:08:00
However, when I try to perform this on the entire(consults_dates), I get an error, e.g.
c= parse_date_time(consults_dates,orders="dmy HM") gives error:
Warning message:
All formats failed to parse. No formats found.
Apologies - if this is blatantly a simple question, day 1 of R after years of Matlab.
You need to pass the column to parse_date_time function and not the entire dataframe.
library(lubridate)
consults_dates$colum_name <- parse_date_time(consults_dates$colum_name, "dmy HM")
However, if you have only one format in the column you can use dmy_hm
consults_dates$colum_name <- dmy_hm(consults_dates$colum_name)
In base R, we can use :
consults_dates$colum_name <- as.POSIXct(consults_dates$colum_name,
format = "%d/%m/%Y %H:%M", tz = "UTC")

Convert UNIX time string to date in R

I have the following UNIX time string:
"1575824800.169"
If you convert this time you will get: 12/08/2019 17:06
on an online unix converter.
However when trying to convert this in R using the following code:
as.POSIXct("1575824800.169", format='%d/%m/%Y %H:%M', origin = "1970-01-01")
I am returned with the value NA
i'm struggling to see why the above code does not work - i have looked into different answers on here, but have not found one where the unix time string has 3 digits after the period (dot) in the string. Maybe this is the problem?
You don't have a string encoding a date (as implied by using the format argument of as.POSIXct) but a number. If we re-cast the string as a numeric and get rid of the format argument we get the expected result (although we might need to use the tz argument to specify a timezone)
as.POSIXct(1575824800.169, origin = "1970-01-01")
Returns:
[1] "2019-12-08 18:06:40 CET"
Edit:
Adding timezone argument
as.POSIXct(1575824800.169, origin = "1970-01-01", tz = "UCT")
Returns:
[1] "2019-12-08 17:06:40 UTC"
Edit 2:
Regarding converting the string to numeric with as.numeric: As #IceCreamToucan pointed out, it does not matter. Only the "printed" value changes, the internal representation stays the same and therefore the result is still correct
as.POSIXct(as.numeric("1575824800.169"), origin = "1970-01-01", tz = "UCT")
Returns the same:
[1] "2019-12-08 17:06:40 UTC"
The anytime package aims to help here with some built-in heuristics. So numeric data in that range is automagically taken as (fractional) seconds since the epoch:
R> anytime::anytime(1575824800.169)
[1] "2019-12-08 11:06:40.168 CST"
R>
There is also a wrapper for UTC and some other options should you need them:
R> anytime::utctime(1575824800.169)
[1] "2019-12-08 17:06:40.168 UTC"
R>

Convert factors to datetime, <fctr> 10/25/2018 (M, D, Y)

I have a data frame called RequisitionHistory2 with a variable called RequisitionDateTime and the levels are factors which look like 4/30/2019 14:16 I would like to split this into RequisitionDate and RequisitionTime in a datetime format.
I tried this code, but this still does not solve my issue with needing to split these into their own columns. The code also did not work as I got the error below.
mutate(When = as.POSIXct(RequisitionHistory2, format="%m/%d/%. %H:%M %p"))
Error in as.POSIXct.default(RequisitionHistory2, format = "%m/%d/%. %H:%M %p") : do not know how to convert 'RequisitionHistory2' to class “POSIXct”
I would like to have the variable RequisitionDateTime split into RequisitionDate and another variable RequisitionTime in the dataframe RequisitionHistory2. Any help is greatly appreciated!
Do not convert factors to datetime directly. You will need to convert it to a character first and then use a datetime function.
as.Date(as.character("10/25/2018"), format = "%m/%d/%Y")
would work for your date example.
library(lubridate)
mutate(df,When = mdy_hm(RequisitionHistory2))
If your datetime is in 4/30/2019 14:16 format
Note that as.POSIXct() works only on datetimes already in ISO 8601 format. I wrote a blog post about this and I think would be helpful for you to check out:
https://jackylam.io/tutorial/uber-data/
The anytime package ON CRAN directly converts from many formats, including factor and ordered to dates and datetime objects. It also heuristically tries a number of viable formats so that you do not need a format string. See the README at GitHub for an introduction, there is also a vignette
Your example works:
R> library(anytime)
R> anytime(as.factor("4/30/2019 14:16"))
[1] "2019-04-30 14:16:00 CDT"
R> anytime(as.factor("4/3/2019 14:16:17"), useR=TRUE)
[1] "2019-04-03 14:16:17 CDT"
R>
However, the underlying (Boost C++) parser does not like single digit days or month so you may need to flip back to R's parser via useR=TRUE as I did on the second example.

convert from posix timestamp in %Y-%m-%d in R

i have date in this format:
1397758632
and i need to convert them into dates in this format %Y-%m-%d.
how can i do this? my timezone is GTM.
Using as.POSIXct() function in this way:
as.POSIXct(x="1397758632",origin="1960-01-01", tz="GMT")
i have this error
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
how can i obtain a readable data?
A couple of things, if you have defined a variable x = "1397758632" you don't have to declare it again, just use x. eg// as.POSIXct(x,origin="1970-01-01", tz="GMT")
2nd, x="1397758632" is actually a char variable, try str(x) you would want to declare it as numeric, x=1397758632 and you should be all good.
Lastly, add as.Date() if you just want the date and not the whole time stamp.
So something like:
x=1397758632
as.POSIXct(x,origin="1970-01-01", tz="GMT")
as.Date(as.POSIXct(x,origin="1970-01-01", tz="GMT")) # for just the date

R dates "origin" must be supplied

My code:
axis.Date(1,sites$date, origin="1970-01-01")
Error:
Error in as.Date.numeric(x) : 'origin' must be supplied
Why is it asking me for the origin when I supplied it in the above code?
I suspect you meant:
axis.Date(1, as.Date(sites$date, origin = "1970-01-01"))
as the 'x' argument to as.Date() has to be of type Date.
As an aside, this would have been appropriate as a follow-up or edit of your previous question.
My R use 1970-01-01:
>as.Date(15103, origin="1970-01-01")
[1] "2011-05-09"
and this matches the calculation from
>as.numeric(as.Date(15103, origin="1970-01-01"))
So generally this has been solved, but you might get this error message because the date you use is not in the correct format.
I know this is an old post, but whenever I run this I get NA all the way down my date column. My dates are in this format 20150521 – NealC Jun 5 '15 at 16:06
If you have dates of this format just check the format of your dates with:
str(sides$date)
If the format is not a character, then convert it:
as.character(sides$date)
For as.Date, you won't need an origin any longer, because this is supplied for numeric values only. Thus you can use (assuming you have the format of NealC):
as.Date(as.character(sides$date),format="%Y%m%d")
I hope this might help some of you.
Another option is the lubridate package:
library(lubridate)
x <- 15103
as_date(x, origin = lubridate::origin)
"2011-05-09"
y <- 1442866615
as_datetime(y, origin = lubridate::origin)
"2015-09-21 20:16:55 UTC"
From the docs:
Origin is the date-time for 1970-01-01 UTC in POSIXct format. This date-time is the origin for the numbering system used by POSIXct, POSIXlt, chron, and Date classes.
If you have both date and time information in the numeric value, then use as.POSIXct. Data.table package IDateTime format is such a case. If you use fwrite to save a file, the package automatically converts date-times to idatetime format which is unix time. To convert back to normal format following can be done.
Example: Let's say you have a unix time stamp with date and time info: 1442866615
> as.POSIXct(1442866615,origin="1970-01-01")
[1] "2015-09-21 16:16:54 EDT"
by the way, the zoo package, if it is loaded, overrides the base as.Date() with its own which, by default, provides origin="1970-01-01".
(i mention this in case you find that sometimes you need to add the origin, and sometimes you don't.)

Resources