Is There a Time-Only R Class

Is there a Time-only R class?

Package data.table has a new class ITime which solves this problem ideally.

library(data.table)
> as.ITime("12:00")-as.ITime("11:10:01")
[1] "00:49:59"

Want only the time portion of a date-time object in R

Once you use strptime you will of necessity get a date-time object and the default behavior for no date in the format string is to assume today's date. If you don't like that you will need to prepend a string that is the date of your choice.

@James' suggestion is equivalent to what I was going to suggest:

format(all_symbol$Time[j], format="%H:%M:%S")

The only package I know of that has time classes (i.e time of day with no associated date value) is package:chron. However I find that using format as a way to output character values from POSIXt objects lends itself well to functions that require factor input.

In the decade since this was written there is now a package named “hms” that has some sort of facility for hours, minutes, and seconds.

hms: Pretty Time of Day

Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class.

Storing time without date but not as class character

The chron package has a "times" class that might be helpful for you. Starting with something similar to what you have so far:

x <- c("1:30 AM", "6:29 AM", "6:59 AM", "9:54 AM", "10:14 AM", "3:15 PM"))
a <- as.POSIXct(x, tz = "", format = "%I:%M %p", usetz = FALSE)

Then we can use the times function with format

library(chron)
(tms <- times(format(a, "%H:%M:%S")))
# [1] 01:30:00 06:29:00 06:59:00 09:54:00 10:14:00 15:15:00
attributes(tms)
# $format
# [1] "h:m:s"
#
# $class
# [1] "times"

Which R time/date class and package to use?

(I am moving this from the comments to the answer portion of stackoverflow at the request of the original poster.)

There is an article in R News 4/1 ("R Help Desk", p. 29) that specifically compares Date, POSIXct and chron. (The 1st two are in core of R and chron is a package.)

timeDate class (in the timeDate package) is based on POSIXct but has extra time zone/financial center support.

For regularly spaced series the the tis package supports many notions of dates.

The mondate package supports accounting dates.

The zoo time series package supports just about any date/time class and also has yearmon and yearqtr for ts compatibility.

The xts time series package works on top of zoo and handles the most common date/time classes by translating them to POSIXct and back again.

There is also information in the Time Series CRAN Task View.

Date time conversion and extract only time

If your data is

a <- "17:24:00"

b <- strptime(a, format = "%H:%M:%S")

you can use lubridate in order to have a result of class integer

library(lubridate)
hour(b)
minute(b)

# > hour(b)
# [1] 17
# > minute(b)
# [1] 24

# > class(minute(b))
# [1] "integer"

and you can combine them using

# character
paste(hour(b),minute(b), sep=":")

# numeric
hour(b) + minute(b)/60

for instance.

I would not advise to do that if you want to do any further operations on your data. However, it might be convenient to do that if you want to plot the results.

How to convert 'time' column to a dedicated time class in R

The column type is 'hms' and not character. You can convert it to POSIXct class which will add the 1st date ie. 1970-01-01 to it.

library(dplyr)

data %>%
mutate(across(ends_with('time'), ~as.POSIXct(., tz = 'UTC')),
across(ends_with('time'),
~as.numeric(format(., '%H.%M')), .names = '{col}2'))

# trip_id start_time end_time start_time2 end_time2
# <dbl> <dttm> <dttm> <dbl> <dbl>
# 1 1 1970-01-01 20:21:42 1970-01-01 21:04:00 20.2 21.0
# 2 2 1970-01-01 08:28:42 1970-01-01 08:58:42 8.28 8.58
# 3 3 1970-01-01 18:41:59 1970-01-01 18:50:59 18.4 18.5
# 4 4 1970-01-01 10:06:15 1970-01-01 11:39:22 10.1 11.4
# 5 5 1970-01-01 08:20:27 1970-01-01 08:51:33 8.2 8.51
# 6 6 1970-01-01 11:11:36 1970-01-01 12:12:54 11.1 12.1
# 7 7 1970-01-01 19:54:38 1970-01-01 20:28:38 19.5 20.3
# 8 25854 1970-01-01 11:33:22 1970-01-01 12:05:06 11.3 12.0
# 9 25855 1970-01-01 07:38:07 1970-01-01 07:40:07 7.38 7.4
#10 25856 1970-01-01 11:33:03 1970-01-01 11:42:02 11.3 11.4

R: How to handle times without dates?

Use the "times" class found in the chron package:

library(chron)

Enter <- c("09:12", "17:01")
Enter <- times(paste0(Enter, ":00"))

Exit <- c("10:15", "18:11")
Exit <- times(paste0(Exit, ":00"))

Exit - Enter # durations

sum(Enter < "10:00:00") # no entering before 10am
mean(Enter < "10:00:00") # fraction entering before 10am

sum(Exit > "17:00:00") # no exiting after 5pm
mean(Exit > "17:00:00") # fraction exiting after 5pm

table(cut(hours(Enter), breaks = c(0, 10, 17, 24))) # Counts for indicated hours
## (0,10] (10,17] (17,24]
## 1 1 0

table(hours(Enter)) # Counts of entries each hour
## 9 17
## 1 1

stem(hours(Enter), scale = 2)
## The decimal point is at the |

## 9 | 0
## 10 |
## 11 |
## 12 |
## 13 |
## 14 |
## 15 |
## 16 |
## 17 | 0

Graphics:

tab <- c(table(Enter), -table(Exit))  # Freq at each time.  Enter is pos; Exit is neg.
plot(times(names(tab)), tab, type = "h", xlab = "Time", ylab = "Freq")
abline(v = c(10, 17)/24, col = "red", lty = 2) # vertical red lines
abline(h = 0) # X axis

screenshot



Related Topics



Leave a reply



Submit