Convert UNIX time string to date in R - 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>

Related

reformatting datetime in R

I have data "A" in the format chr "5/7/2021 15:15". I would like to convert it to a format which R will recognize. (It is giving me errors when I try to plot, for instance, which leads me to believe it needs to be reformatted.)
Here is the format "B" I would like to achieve. R seems to like this ok, so I might as well match it (?):
POSIXct, format: "2021-8-11 16:00:00". I am not sure if the seconds are needed, and they do not exist in data "A" so the seconds could be omitted. If R doesn't care then I don't either. The timezone is UTC.
How do I do it? I have tried a couple things, including:
CTD_datetime_UTC <- as.POSIXct(CTD$Date.and.Time, tz = "UTC").
You can use strptime from base R. But there are many parsers for dates...
Assuming the format is "day/month/year" (example is not unambiguous, could also be "month/day/year")
strptime("5/7/2021 15:15", "%d/%m/%Y %H:%M", tz = "UTC")
Returns:
[1] "2021-07-05 15:15:00 UTC"
Using parsedate
library(parsedate)
parse_date("5/7/2021 15:15")
[1] "2021-05-07 15:15:00 UTC"

How to convert a datetime to date in R, WITHOUT rounding the day?

I'm using R and trying to convert a datetime field into just the date? R gives me the desired format but keeps rounding up some of the day values. Specifically everything after 12 noon! I could not find any threads that address this exact problem. I actually figured out a solution but wanted to post the question because I spent a whole week troubleshooting.
#Convert the datetime field from character to a datetime
main_df$datetime <- strptime(main_df$ï..Date, format = "%m/%d/%Y %H:%M")
main_df$datetime <- as.POSIXct(main_df$datetime, tz = Sys.timezone())
head(main_df$datetime)
class(main_df$datetime)
#Remove the poorly computer-titled character field that contained datetime info
main_df <- subset(main_df, select = -c(ï..Date))
#Use the NEW datetime field to create a date field
#main_df$Date <- trunc(main_df$datetime,"days")
main_df$Date <- as.Date(main_df$datetime, format = "%m/%d/%Y")
?as.Date()
class(main_df$Date)
head(main_df$Date)
That returned:
head(main_df$datetime)
[1] "2020-05-16 00:31:00 CDT" "2020-05-16 00:30:00 CDT" "2020-05-15 23:33:00 CDT" "2020-05-15 15:33:00 CDT"
[5] "2020-05-15 22:31:00 CDT" "2020-05-15 22:12:00 CDT"
and
> class(main_df$Date)
[1] "Date"
>
> head(main_df$Date)
[1] "2020-05-16" "2020-05-16" "2020-05-16" "2020-05-15" "2020-05-16" "2020-05-16"
Notice how the last 4 values for 'Date' should be 2020-05-15 but instead, they are converted to be 2020-05-16. So what are some other ways to fix this? I'm going to post one way that worked but I doubt it's the cleanest.
If we don't want to make use of the 'time', then use a regex to match a space followed by other characters (" .*"), replace with blank in sub and then convert to Date class. The issue with converting to DateTime is that there are times "23:33:00" that would make it convert to next day
main_df$Date <- as.Date(sub(" .*", "", main_df$datetime), format = "%m/%d/%Y")
Do your input dates include a timezone specification? If not, they are ambiguous and the rounding may be right or it may be wrong. If they do include a timezone specification, the lubridate package should handle them correctly.
I would advise against using tz = Sys.timezone() because that would make the interaction between input data and algorithm dependent on geography if your inputs don't include a timezone specification, so what works for you might not work for a different user in a different location.
You could just extract date from datetime as a substring and convert it to Date type, using substr(x,begin,end) function, where x - your column, begin and end - begin and end of string to extract.
main_df$Date <- as.Date(substr(main_df$datetime,1,10))
I had the same issue and that function helped me to convert Datetime to Date without rounding.

From number to formatted datetime in R

I have a field into a dataframe of class numeric.
I want to convert that into a date time format.
value: 1353959527000000
expected: 2012-11-26 11:52:07.000-08:00
How do I do that in R?
I tried:
Using lubridate or default Posix conversion and nothing produced the date above. Read a bunch of posts and still not figuring out what I am doing wrong.
dn <- 1353959527000000
as.POSIXct(as.numeric(as.character(dn)),origin="1970-01-01 00:00:00")
output was something super off the expected date with some gibberish.
Same output trying this
as_datetime(1353959527000000, origin = "1970-01-01 00:00:00")
It's FAQ and a repeat question, but as #r2evans told you, the right scale helps. As does eg anytime::anytime as it frees you from using the origin etc pp:
R> dn <- 1353959527000000
R> anytime::anytime(dn/1e6) # local time
[1] "2012-11-26 13:52:07 CST"
R> anytime::utctime(dn/1e6) # utctime
[1] "2012-11-26 19:52:07 UTC"
R>

Date-time to numeric and backwards

Dates/Times in R
I have this date: "2016-10-29 15:00:00" and i want to convert it to numeric and backwards to the same date and time i had. I used this to convert it to numeric:
as.numeric(as.POSIXct("2016-10-29 15:00:00"))
How i can get back my initial date and time?
"2016-10-29 15:00:00"
as.numeric(as.POSIXct("2016-10-29 15:00:00"))
1477771200
I obtain that answer, but i need it back to "2016-10-29 15:00:00". What should i do?
You can use as.POSIXct() on your number, but you also need to supply the origin and (probably) timezone
as.POSIXct(1477713600, origin = "1970-01-01", tz = "Australia/Melbourne")
"2016-10-29 15:00:00 AEDT"
The as.POSIX() comamnd needs to know from which reference point the numeric starts. This is usually the Unix Epoch of 1970-01-01
The documentation for ?as.POSIXct shows the useage for a numeric object
S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)
showing you need to supply the origin
This should work.
as.POSIXct(yourNumeric)
Where yourNumeric is your number.

Problems converting variable into proper datetime format in 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"

Resources