Lubridate - how to properly parse decimal minutes using parse_date_time [duplicate] - r

This question already has an answer here:
Converting numeric time to datetime POSIXct format in R
(1 answer)
Closed 2 years ago.
Let's say that you have a vector of times in minutes stored as decimal numbers. For this example, I am going to pick 2.5, which means 2 minutes and 30 seconds.
I want to parse this using lubridate's parse_date_time function. However, the output I see is wrong. After running this line:
parse_date_time(2.5, "M:S")
I am getting the following output:
"0000-01-01 00:02:05 UTC"
Instead of expected
"0000-01-01 00:02:30 UTC"
Somehow lubridate doesn't recognize that my .5 doesn't mean 5 seconds, but 30 seconds since two and a half minutes are 2 minutes and 30 seconds and not 2 minutes and 5 seconds.
What is the proper format of the parse_date_time I should use to get the expected output?

Here's a base R option :
val <- 2.5
as.POSIXct(val*60, origin = '0000-01-01', tz = 'UTC')
#[1] "0000-01-01 00:02:30 UTC"

if you want to use lubridate you can do something like this
library(lubridate)
time <- 2.5
as.period(as.duration(days(x=1))*(time/24))
#[1] "2H 30M 0S"
as.period(as.duration(days(x=1))*(time/1440))
#[1] "2M 30S"

Related

Convert from Unix Epoch time (in microseconds) to DateTime in R [duplicate]

This question already has an answer here:
Converting timestamp in microseconds to data and time in r
(1 answer)
Closed 3 years ago.
I have dates formatted like so: 1.475534e+15, which when converted via https://www.epochconverter.com/ convert to Monday, October 3, 2016 10:33:20 PM
However, I cannot replicate this in R.
For example:
library(anytime)
anytime(1.475534e+15)
Yields "46759781-01-30 14:33:20 EST"
The same is true if I do something like
as.POSIXct(1.475534e+15 / 1000, origin="1970-01-01")
The epochconverter site suggests that the time is in microseconds, but I haven't figured out how to convert from microseconds to a human-readable date.
a <- 1.475534e+15
as.POSIXct(a/1000000, origin="1970-01-01")
#[1] "2016-10-03 15:33:20 PDT" # interpreted in my local tz
With 7 significant digits in the scientific notation, that gets us around 20 minutes of time resolution. If you need more than that, you'll need to get the data in different format upstream.

Recognize time in R

I am working on a dataset in R with a time variable like this:
Time = data.frame("X1"=c(930,1130,914,1615))
The first one/two digits of X1 refers to hour and the last two refers to minute. I want to make R recognize it as a time variable.
I try to use lubridate hm function but it didnt work probably because a ":" is missing between the hour and minute in my data.
I also thought about using str_sub function to separate the hour and minute first and then put them together with a ":" in between and finally use the lubridate function but I dont know how to extract the hour since sometimes it is presented as one digit but sometimes it is presented as two digits.
How do I make R recognize this as a time variable?
Thanks very much!
You could 0-pad to 4 digits and then format using standard R date tools:
as.POSIXct(sprintf("%04d",Time$X1), format="%H%M")
#[1] "2018-04-22 09:30:00 AEST" "2018-04-22 11:30:00 AEST"
#[3] "2018-04-22 09:14:00 AEST" "2018-04-22 16:15:00 AEST
This converts them to chron "times" class. Internally such variables are stored as a fraction of a day and are rendered on output as shown below. The sub inserts a : before the last 2 characters and :00 after them so that they are in HH:MM:SS format which times understands.
library(chron)
times(sub("(..)$", ":\\1:00", Time$X1))
## [1] 09:30:00 11:30:00 09:14:00 16:15:00
It could also be done like this where we transform each to a fraction of a day:
with(Time, times( (X1 %/% 100) / 24 + (X1 %% 100) / (24 * 60) ))
## [1] 09:30:00 11:30:00 09:14:00 16:15:00

Convert a decimal number to HH:MM:SS in R

I have a series of decimal numbers (marathon race split times): 64.90, etc., and I want to convert it into HH:MM:SS format using R so that I can us the result to do time math. The answer I am looking for is: 1:04:54.
chron doesn't seem to be doing what I'm expecting it to do.
chron::times(64.90)
Time in days:
[1] 64.9
First time on this site, so be kind. Thanks.
chron times are measured in days and since you apparently have minutes divide the input by the number of minutes in a day:
library(chron)
times(64.90 / (24 * 60))
## [1] 01:04:54
You could try lubridate::seconds_to_period
library(lubridate)
seconds_to_period(64.90)
[1] "1M 4.90000000000001S"
library(hms)
as.hms(64.90*60)
output
01:04:54

Converting midnight in to a date time format in R

I have a very specific problem. I have been trying to convert a date time character into a date time format in R. Example: "2017-05-21 00:00:00".
Whenever I try to convert it using strptime and as.POSIXct to a date time format it gives me "2017-05-21".
Thanks for any help
As #ngm says, this is only a formatting choice on the part of R. You can check to make sure it's actually midnight. Datetimes are stored as seconds past the epoch, and can actually be used in arithmetic.
t1 <- as.POSIXct("2017-05-21 00:00:00")
t1
# [1] "2017-05-21 EDT"
as.integer(t1)
# [1] 1495339200
So your time is 1,495,339,200 seconds after the epoch. Now we can look at midnight plus one second.
t2 <- as.POSIXct("2017-05-21 00:00:01")
t2
# [1] "2017-05-21 00:00:01 EDT"
as.integer(t2)
# [1] 1495339201
Which is one second higher than t1. So t1 is, in fact, midnight.

In R, use lubridate to convert hms objects into seconds

simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day.
For instance
library(lubridate)
hms("12:34:45")
then I want to know exactly how long 12 hours, 34 minutes, and 45 seconds is, in seconds
something obvious like
seconds(hms("12:34:45"))
just returns
45s
which is not what I want. How do I convert these hms values into seconds? I'd like to use lubridate
R>lubridate::period_to_seconds(hms("01:00:00"))
gives expected 3600 seconds as numeric counting from 00:00:00
or in the case above:
R>period_to_seconds(hms("12:34:45"))
It doesn't matter which package you use -- it will have convert a date / datetime object into a POSIXct representation of seconds since the epoch. So you may as well do it in base R -- so here deploy ISOdatetime() with an arbitrary day, using today:
R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0))
Time difference of 12.5792 hours
So we want seconds:
R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0),
+ unit="secs")
Time difference of 45285 secs
And we can cast to numbers:
R> as.numeric(difftime(ISOdatetime(2012,7,2,12,34,45), +
ISOdatetime(2012,7,2,0,0,0), unit="secs"))
[1] 45285
Edit: And getting back to lubridate, this is arguably a bug:
> hms("12:34:45") - hms("00:00:00")
[1] 12 hours, 34 minutes and 45 seconds
R> as.numeric(hms("12:34:45") - hms("00:00:00"))
[1] 45
R>

Resources