从时间中提取小时的最快方法(HH:MM) [英] Fastest way to extract hour from time (HH:MM)

查看:439
本文介绍了从时间中提取小时的最快方法(HH:MM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望 fastPOSIXct 可以工作-但在这种情况下不起作用。

Wish fastPOSIXct works - but not working in this case.

这是我的时间数据(没有日期)-我需要从中获取小时数。

Here is my time data (which does not have dates) - and I need to get the hours-part from them.

times <- c("9:46","11:06", "14:17", "19:53", "0:03", "3:56")

这是 fastPOSIXct 的错误输出:

fastPOSIXct(times, "GMT")
[1] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"
[3] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"
[5] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"

data.table hour 方法c $ c>和 as.ITime 可以解决此问题,但是在大时间数组上看起来很慢。

The hour method from data.table with as.ITime solves the purpose, but looks like slow on large times arrays.

library(data.table)
hour(as.ITime(times))
# [1]  9 11 14 19  0  3

想知道是否有更快的方法(就像 fastPOSIXct 一样,但是不需要日期即可使用。)

Wondering if there is some faster way (just like fastPOSIXct, but works without the need for date).

fastPOSIXct 的确像snap一样工作,但只是错误。

fastPOSIXct really works like snap, but just wrong.

推荐答案

您还可以尝试 substr as.integer(substr(vals,start = 1,stop = nchar(vals)-3 ))

在具有10e6个元素的向量的基准中, stringi :: stri_sub 最快,而 substr 第二。

In a benchmark on a vector with 10e6 elements, stringi::stri_sub is fastest, and substr number two.

vals <- sample(c("9:46", "11:06", "14:17", "19:53", "0:03", "3:56"), 1e6, replace = TRUE)

fun_substr <- function(vals) as.integer(substr(vals, start = 1, stop = nchar(vals) - 3))

grab.hrs <- function(vals) as.integer(sub(pattern = ":.*", replacement = "", x = vals))

fun_strtrim <- function(vals) as.integer(strtrim(vals, nchar(vals) - 3))

library(chron)
fun_chron <- function(vals) hours(times(paste0(vals, ":00")))

fun_lt <- function(vals) as.POSIXlt(vals, format="%H:%M")$hour

library(stringi)
fun_stri_sub <- function(vals) as.integer(stri_sub(vals, from = 1, to = -4))

library(microbenchmark)
microbenchmark(fun_substr(vals),
               fun_stri_sub(vals),      
               grab.hrs(vals),
               fun_strtrim(vals),
               fun_lt(vals),
               fun_chron(vals),
               unit = "relative", times = 5)
# Unit: relative
#               expr       min        lq      mean    median        uq       max neval
#   fun_substr(vals)  2.186714  1.902074  2.015082  1.968542  1.945007  2.090236     5
# fun_stri_sub(vals)  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000     5
#     grab.hrs(vals)  2.656630  2.397918  2.687133  2.426223  2.446902  3.263962     5
#  fun_strtrim(vals) 31.177869 27.601380 26.009818 27.423562 17.902507 29.426989     5
#       fun_lt(vals) 47.296929 41.122287 42.266556 40.647465 30.539030 52.710992     5
#    fun_chron(vals)  5.594931  5.159192  5.961775  7.746242  5.286944  6.189742     5

这篇关于从时间中提取小时的最快方法(HH:MM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆