简体   繁体   English

从 data.table 库中取整/取整 ITime 格式时间的有效方法是什么

[英]What is the efficient way to round/floor ITime-formated time from data.table library

What is the efficient way to round/floor ITime-formated time from data.table library?data.table库中舍入/计算 ITime 格式时间的有效方法是什么?

The way I convert it is: I transform it to POSIXct, floor the result, and then convert it back to ITime.我转换它的方式是:我将其转换为 POSIXct,将结果取底,然后将其转换回 ITime。 Example例子

library(lubridate)
library(data.table)

# Suppose I have some ITime variable:
Time = as.ITime( Sys.time() )

#That's what I do:
as.ITime( floor_date( as.POSIXct( Time ), "5 minutes"), format = "%H:%M:%S")

#Result:
[1] "16:05:00"

That works OK, but does not seem efficient because of the double converting.这工作正常,但由于双重转换似乎效率不高。 Is there a good alternative?有没有好的选择?

You could use the fact that an ITime variable is internally stored as an integer (number of seconds).您可以使用ITime变量在内部存储为 integer(秒数)这一事实。

library(lubridate)
library(data.table)

# Let generate an ITime variable

set.seed(233)
y <- as.ITime(sample(60*60*24, size = 1e6, replace = TRUE))  # 60*60*24: max number of seconds in a day 

because 5 minutes are 60 * 5 seconds (300 seconds), you could divide your variable by 300, take its floor and then multiply back by 300. You can use the integer division operator, %/% , for the first two steps.因为 5 分钟是 60 * 5 秒(300 秒),所以您可以将变量除以 300,取其下限,然后再乘以 300。您可以在前两个步骤中使用 integer 除法运算符%/%

# head of the data using this method  and the one you suggested:
head(data.table(
    y = y,
    method1 = (y %/% 300L) * 300L,
    method2 = as.ITime( floor_date( as.POSIXct( y ), "5 minutes"), format = "%H:%M:%S")),
    n = 10)
  
           y  method1  method2
 1: 13:21:33 13:20:00 13:20:00
 2: 13:24:11 13:20:00 13:20:00
 3: 18:02:47 18:00:00 18:00:00
 4: 20:06:51 20:05:00 20:05:00
 5: 19:59:35 19:55:00 19:55:00
 6: 16:35:46 16:35:00 16:35:00
 7: 16:32:10 16:30:00 16:30:00
 8: 15:57:35 15:55:00 15:55:00
 9: 01:21:16 01:20:00 01:20:00
10: 17:10:09 17:10:00 17:10:00

Timing定时

microbenchmark::microbenchmark(
  method1 = (y %/% 300L) * 300L,
  method2 = as.ITime( floor_date( as.POSIXct( y ), "5 minutes"), format = "%H:%M:%S"),
  times = 5L
)

Unit: milliseconds
    expr      min       lq      mean   median       uq      max neval
 method1   7.5192   7.7691   8.23544   8.0286   8.8695   8.9908     5
 method2 396.5867 404.5420 418.07694 412.6798 436.3783 440.1979     5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建data.table :: ITime向量 - creating data.table::ITime vector 在data.table中创建ITime间隔 - Create ITime intervals in data.table 在匹配来自第二个表的数据时,在`data.table`中创建向量列的最有效方法是什么? - What is the most efficient way to create a column of vectors in `data.table` when matching data from a second table? 用另一个 data.table 的相关值替换 data.table 列中的向量值的最有效方法是什么? - What is the most efficient way to replace a vector's values in a data.table's column with correlating values from another data.table? 从不同的data.table向data.table的列分配值的最“ data.table”方法是什么 - What is the most “data.table” way to assign values to a column of a data.table from a different data.table 具有正确排序的时间戳的data.table中行绑定时间序列的有效方式 - Efficient way of row binding time series in a data.table, with correctly sorted timestamps 使用字典替换data.table中的值的最有效方法是什么? - What is the most efficient way to replace values in a data.table using a dictionary? 在R data.table中粘贴多个列对的有效方法 - Efficient way to paste multiple column pairs in R data.table 在 R data.table 中对两个测试进行评分的有效方法 - An efficient way to score two tests in an R data.table 根据聚合值过滤 data.table 的最有效方法 - Most efficient way to filter data.table based on an aggregated value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM