lubridate - get hours and minutes from decimal hours - r

I have decimal hours in format 245.85 equalling to 245:51:00 in [hh]:mm:ss format.
I want to transform the decimal hours to hh:mm format, but how do I do it?
the original calculation that renders 245.85 is:
library(lubridate)
time_length(hm("7 27")*33,unit = "hours")
what I want is 245:51 or 245:51:00
If I use as.period I get days too - like in:
as.period(dhours(time_length(hm("7 27")*33,"hours")))
[1] "10d 5H 51M 0S"
and for background - my aim is to multiply hours and minutes (e.g. 7:27) by an arbitrary integer (e.g. 33) and get result back in hh:mm format - avoiding days (as in as.period example above). Say if a piece of work takes 7 hours and 27 minutes and we give me 33 pieces of such work to do per year, it should take me about this many work hours (and minutes) to do.

If it's really only the H:M:S format that gives you trouble, try
library(hms)
hms(hours=245.85)
which yields 245:51:00

Related

In R convert a list of numbers that represent time in hours to a POSIXct or Date variable format

I have a list of times that are an output of a model, these are in a decimal number format and represent the time from the start of the model running in hours, but they have not been given any units in the output, they are just numbers.
What I would like to do is convert these numbers into a date stamp format, using the numbers as hours the model has run. Specifically as either a POSIXct or Date variable so I can start use the Bupar library.
So for example I would like to convert 1.75, into 1 hour 45 minutes and 25.5 into a time that would equal 1 day 1 hour and 30 mins.
Thanks for any help
One possible way:
We could use seconds_to_period() function from lubridate after multiplying by 3600 to get seconds:
library(lubridate)
x <- 1.75
seconds_to_period(x*3600)
#[1] "1H 45M 0S"

adding hours and minutes to 24 hour time in R

Im trying to find a way to create a column of the hours of arrival of different individuals from 18:30, by adding the hours and minutes to 18:30, but it doesnt work, and each time I try to convert the column format, of the hours and minutes I want to add, from "factor" to a time format (such as chron or POSIXct)it adds today`s date and I want to add only the hours and minutes without considering date.
For example, I want to add 03:38 hours to 18:30, which will be 22:08. So, I want to find a way to have a column with all the final hours (hours of arrival of each individual). another example is adding 00:47 hours to 18:30 which is 19:17.
I tried to do it by converting the hours+minutes to a numeric format and than add it to 18.5 (which is 18:30, just in a numeric format), and I reached final hours, but they are in a numeric format, and I wish them to be in an hour format. For example:
I added 3.6425980 (which is 03:38) to 18.5 (which is 18:30) and the final hour was 22.14260 (which is 22:08), but I don`t know how to convert 22.14260 to 22:08.
I need to do it for a lot of data, so I need a code that does one of the above things.
Can someone help me?
thanks!
One option is to use lubridate
time <- c("18:30", "18:30")
duration <- c("3:38", "00:47")
library(lubridate)
hms(hm(time) + hm(duration), roll = T)
#[1] "22H 8M 0S" "19H 17M 0S"

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

Date string conversion from R to CSV in Libre Calc

I'm trying to get acquainted with weatherData in R.
Having downloaded a set of temperature data I've then exported it to CSV.
Opening the CSV in Libre Calc shows the date and time for each temperature reading as a string of ten digits. In spite of some Googling I have not found a way of successfully converting the string into the format in which it appears in R.
For example: 1357084200 I believe should translate to 2013-01-01 23:50:00
Any help in getting the correct date in the same date format to appear in Calc via the CSV greatly appreciated.
Here is the direct way:
as.POSIXct(1357084200, origin="1970-01-01", tz="GMT")
#[1] "2013-01-01 23:50:00 GMT"
If it's really a character:
as.POSIXct(as.numeric("1357084200"), origin="1970-01-01", tz="GMT")
I'm not aware of a direct way of doing this, but I believe I've figured out a workaround.
For starters your example is correct. The long number (timestamp) is the number of seconds passed since 1970-01-01 00:00:00. Knowing this you can actually calculate the exact date and time from the timestamp. It's a bit complicated due to needing to take into account the leap years.
What comes in handy is the ability to supply an arbitrary number of days/months/years to LibreOffice function DATE. So in essence you can find out the number of days represented in timestamp by dividing it by 60*60*24 (number of seconds in a minute, number of minutes in an hour, number of hours in a day). And then supply that number to the date function.
timestamp = 1357084200
days = timestamp / FLOOR(timestamp / (60*60*24); 1) // comes out at 15706
actualdate = DATE(1970; 1; 1 + days) // comes out at 2013-01-01
seconds = timestamp - days * 60 * 60 * 24 // comes out at 85800
actualtime = TIME(0; 0; seconds) // comes out at 23:50:00
Then you can concatenate these or whatever else you want to do.

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