Convert decimal day to HH:MM - r

I have a vector of decimal numbers, which represent 'decimal day', the fraction of a day. I want to convert it into HH:MM format using R.
For example, the number 0.8541667 would correspond to 20:30. How can I convert the numbers to HH:MM format?

Using chron:
chron::times(0.8541667)
#[1] 20:30:00

Try this:
R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R>
We start with a date--which can be any date, so we use today--and add your desired fractional day.
We then convert the Date type into a Datetime object.
Finally, we format the hour and minute part of the Datetime object, ensuring that UTC is used for the timezone.

One option with data.table:
> library(data.table)
> structure(as.integer(0.4305556*60*60*24), class="ITime")
[1] "10:20:00"
We convert from day fraction to seconds since midnight; coerce to integer; and apply ITime class. (ITime works with integer-stored seconds since midnight.)
Other resources:
#GaborGrothendieck re chron package and link to his R News article with Thomas Petzoldt about converting from Excel in particular Converting a time decimal/fraction representing days to its actual time in R?
#JorisChau re RStudio's hms package how to convert excel internal coding for hours to hours in R?

Related

Import separate date and time (hh:mm) excel columns, to use for time elapsed calculation

Newbie here, first post (please be gentle). I have been trying to resolve this for several hours, so finally decided time to ask advice.
I have a large spreadsheet which I am importing with readxl. It contains one column with date (format dd/mm/yyyy) and several time columns in format hh:mm as can be seen: excel
Essentially I want to be able to import both time and date columns and combine them, so that I can then do some other calculations, like time elapsed.
If I import letting R guess the col-types, it converts the times to POSIXct, but these then have a date on 1899 attached to them: R_POSIXct
If I force readxl to assign the time column to numeric, I get a decimal (e.g. 0.315972222 for 07:35), which then tried converting using similar syntax to
format(as.POSIXct(Sys.Date() + 0.315972222), "%Y-%m-%d %H:%M:%S", tz="UTC")
i.e.
df$datetime <- format(as.POSIXct(df$date + df$time), "%Y-%m-%d %H:%M", tz="UTC")
which results in the correct date, but with a time of 00:00, not the time it is passed.
I have tried searching here and found posts to be not quite the same question (e.g. Combining date and time columns into dd/mm/yyyy hh:mm), and have read widely, including about about lubridate, but as I'm only 6 months into R, am finding some explanations a bit cryptic.
Suggestions or ignposting appreciated (if there are solutions I haven't found)
If you subtract the number of days between 1899-01-01 and 1970-01-01 and then multiply that (shifted) Excel numeric value by 3600 you should come close to the number of seconds since start of 1970. You could then convert to POSIXct with as.POSIXct( x, origin="1970-01-01"). That does seem to be "the hard way", however
It would be far easier and probably more accurate to convert the date-times to YYYY-MM-DD H:M:S format and then export as csv to be imported into R as text. There is a "POSIXct" colClasses argument to read.csv, although it doesn't handle separate columns of date and time. For that you would be advised to import as character values and then paste the dates and times. Then watch you format strings for as.POSIXct. The dd/mm/yyyy "format" would be specified by "%d/%m/%Y".

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

Convert time (24 hours format) in character type to time in R

I have a data frame with time in character format, I need to convert it into time format. I have tried using strptime and POSIXct but it adds the date also. I just need the time.
For e.g.: TRK_DEP_TIME <- c("22:00", "14:30"......) _____ character datatype
doing........ as.POSIXCT(TRK_DEP_TIME, format = %H:%M")
The result will be ("10/11/17,22:00", "10/11/17, 14:30".....)
I am looking for just the time, I don't need the date to be associated with it. Is there any way I can achieve this?
Use chron "times" class:
library(chron)
ch <- c("22:00", "14:30") # test input (character)
times(paste0(ch, ":00"))
## [1] 22:00:00 14:30:00

Converting timestamp in seconds to a date format in R

I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.
The suggested code for R was:
tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)
But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.
You should us as.POSIXct function instead:
tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")
strptime converts between character representations and dates not between timestamp and dates.
Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.
lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"

How to convert ordinal date day-month-year format using R

I have log files where the date is mentioned in the ordinal date format.
wikipedia page for ordinal date
i.e 14273 implies 273'rd day of 2014 so 14273 is 30-Sep-2014.
is there a function in R to convert ordinal date (14273) to (30-Sep-2014).
Tried the date package but didn come across a function that would do this.
Try as.Date with the indicated format:
as.Date(sprintf("%05d", 14273), format = "%y%j")
## [1] "2014-09-30"
Notes
For more information see ?strptime [link]
The 273 part is sometimes referred to as the day of the year (as opposed to the day of the month) or the day number or the julian day relative to the beginning of the year.
If the input were a character string of the form yyjjj (rather than numeric) then as.Date(x, format = "%y%j") will do.
Update Have updated to also handle years with one digit as per comments.
Data example
x<-as.character(c("14273", "09001", "07031", "01033"))
Data conversion
x1<-substr(x, start=0, stop=2)
x2<-substr(x, start=3, stop=5)
x3<-format(strptime(x2, format="%j"), format="%m-%d")
date<-as.Date(paste(x3, x1, sep="-"), format="%m-%d-%y")
You can use lubridate package as follows:
>library(lubridate)
# Create a template date object
>date <- as.POSIXlt("2009-02-10")
# Update the date using
> update(date, year=2014, yday=273)
[1] "2014-09-30 JST"

Resources