Convert time from numeric to time format in R - r

I read data from an xls file. Apparently, the time is not in the right format. It is as follows (for example)
0.3840277777777778
0.3847222222222222
0.3854166666666667
Indeed, they should be
09:12
09:13
09:13
I don't know how to convert it to the right format. I searched several threads and all of them are about converting the date (with/without time) to the right format.
Can somebody give me any clues?

You can use as.POSIXct after having multiplied your number by the number of seconds in a day (60 * 60 * 24)
nTime <- c(0.3840277777777778, 0.3847222222222222, 0.3854166666666667)
format(as.POSIXct((nTime) * 86400, origin = "1970-01-01", tz = "UTC"), "%H:%M")
## [1] "09:13" "09:14" "09:15"

Another option is times from chron
library(chron)
times(nTime)
#[1] 09:13:00 09:14:00 09:15:00
To strip off the seconds,
substr(times(nTime),1,5)
#[1] "09:13" "09:14" "09:15"
data
nTime <- c(0.3840277777777778, 0.3847222222222222, 0.3854166666666667)

For people who want the opposite way: given the 09:13:00, get 0.3840278
as.numeric(chron::times("09:13:00"))
Essentially, the idea is that one whole day is 1,so noon (12pm) is 0.5.

Related

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

Create new column "Weekday %H:%M" from Timestamp

I have some columns (timestamp, object_id, status and some others) with which I want to predict the status of an object.
I have the hypothesis that the "weektime" has an important influence on the status. Under "weektime" I understand: Monday 23:17.
Now I think I need to create a column with this format to test the hypothesis.
I already converted the timestamp to POSIXlt:
training_data$TimeStamp = as.POSIXlt(training_data$TimeStamp, "", "%Y-%m-%d %H:%M:%OS")
I also already created a column with only the weekday.
training_data$TimeStamp_weekday = weekdays(training_data$TimeStamp)
Can you help me to create a column with the "weektime"?
I think I also need to have only 4 "time slots" of 15min per hour to make the predictions easier. So Monday 23:17 -> 23:15
0-15 mins -> 0
15-30 mins -> 15
30-45 mins -> 30
45-60 mins -> 45
Or something similar.
Don't need to paste anything on to lubridate::weekday results. There is a trunc.POSIXt but it doesn't let you truncate to fractional intervals as far as I can tell. Instead truncate to the prior 15 minute mark by subtracting 7.5 minutes (=.0.125 hours), dividing by the same amount, rounding and then multiplying by that amount. That should have the effect of "rounding down" to the prior interval mark. Then use format.POSIXt to get the desired format.
> Sys.time()
[1] "2017-12-29 12:24:49 PST"
>
format( as.POSIXct( to convert back to datetime
round( as.numeric( Sys.time() -0.125 * 60*60 )/(0.125 * 60*60) ) * 0.125*60*60 ,
origin="1970-01-01"), "%A %H:%M")
[1] "Friday 12:15"
What is does is shift all the times so they are centered on the interval "marks" or boundaries, then rounds to the nearest whole number on that scale, and then expands back to the original scale.

R function to convert time of day to total minutes

I need to take the time of day (e.g. 13:34) and convert it to minute number or number of minutes in a day it represents so the expected answer would be (780 + 34) or 814 minutes
I was thinking about extracting the hours and minutes into variables probably using lubridate and of course, multiplying the hours by 60 and adding the minutes.
But is there a method or function that I can use for this that already exists? Thought I'd check with the SO community.
Thanks
Thanks to rawr - providing a POSIX type answer that worked just fine:
difftime(as.POSIXct('13:34', format = '%H:%M'), as.POSIXct('00:00', format = '%H:%M'), units = 'min')
For everyone here's the output I got:
z<-difftime(as.POSIXct('13:34', format = '%H:%M'), as.POSIXct('00:00', format = '%H:%M'), units = 'min')
#print out z
> z
#response
Time difference of 814 mins
#confirm that z only has the 814 as value
> typeof(z) [1] "double"

Strip the date and keep the time

Lots of people ask how to strip the time and keep the date, but what about the other way around? Given:
myDateTime <- "11/02/2014 14:22:45"
I would like to see:
myTime
[1] "14:22:45"
Time zone not necessary.
I've already tried (from other answers)
as.POSIXct(substr(myDateTime, 12,19),format="%H:%M:%S")
[1] "2013-04-13 14:22:45 NZST"
The purpose is to analyse events recorded over several days by time of day only.
Thanks
Edit:
It turns out there's no pure "time" object, so every time must also have a date.
In the end I used
as.POSIXct(as.numeric(as.POSIXct(myDateTime)) %% 86400, origin = "2000-01-01")
rather than the character solution, because I need to do arithmetic on the results. This solution is similar to my original one, except that the date can be controlled consistently - "2000-01-01" in this case, whereas my attempt just used the current date at runtime.
I think you're looking for the format function.
(x <- strptime(myDateTime, format="%d/%m/%Y %H:%M:%S"))
#[1] "2014-02-11 14:22:45"
format(x, "%H:%M:%S")
#[1] "14:22:45"
That's character, not "time", but would work with something like aggregate if that's what you mean by "analyse events recorded over several days by time of day only."
If the time within a GMT day is useful for your problem, you can get this with %%, the remainder operator, taking the remainder modulo 86400 (the number of seconds in a day).
stamps <- c("2013-04-12 19:00:00", "2010-04-01 19:00:01", "2018-06-18 19:00:02")
as.numeric(as.POSIXct(stamps)) %% 86400
## [1] 0 1 2

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