简体   繁体   English

在data.table中创建ITime间隔

[英]Create ITime intervals in data.table

I have a datetime variable ( vardt ) as a character in large data table. 我有一个日期时间变量( vardt )作为大数据表中的字符。 Eg "21/07/2011 15:54:57" 例如"21/07/2011 15:54:57"

I can turn it into ITime class (eg 15:54:57 ) with DT[,newtimevar:=as.ITime(substr(DT$vardt,12,19))] but I would like to create groups of minutes, so from 21/07/2011 15:54:57 I would obtain 15:54:00 or 15:54 . 我可以用DT[,newtimevar:=as.ITime(substr(DT$vardt,12,19))] 15:54:57 ))把它变成ITime类(例如15:54:57 )但是我想创建分钟组,所以从21/07/2011 15:54:57我会得到15:54:0015:54

I have tried: DT[,cuttime := as.ITime(cut(DT$vardt, breaks = "1 min",))] but it didn't work. 我试过了: DT[,cuttime := as.ITime(cut(DT$vardt, breaks = "1 min",))]但它没有用。 I am reading the zoo package documentation but I haven't found anything yet. 我正在阅读zoo package文档,但我还没有找到任何东西。 Any idea/function that could be useful for this case in a large data table? 在大型数据表中对于这种情况有用的任何想法/功能?

Here are two possible approaches: 以下是两种可能的方法:

library(data.table)
##
x <- Sys.time()+sample(seq(0,24*3600,60),101,TRUE)
x <- gsub(
  "(\\d+)\\-(\\d+)\\-(\\d+)",
  "\\3/\\2/\\1",
  x)
##
DT <- data.table(vardt=x)
##
DT[,time:=as.ITime(substr(vardt,12,19))]
##
DT[,hour_min:=as.ITime(
  gsub("(\\d+)\\:(\\d+)\\:(\\d+)",
       "\\1\\:\\2\\:00",time))]
DT[,c_hour_min:=substr(time,1,5)]
##
R>  head(DT)
                 vardt     time hour_min c_hour_min
1: 28/01/2015 05:38:30 05:38:30 05:38:00      05:38
2: 27/01/2015 14:15:30 14:15:30 14:15:00      14:15
3: 28/01/2015 06:03:30 06:03:30 06:03:00      06:03
4: 28/01/2015 00:37:30 00:37:30 00:37:00      00:37
5: 27/01/2015 17:59:30 17:59:30 17:59:00      17:59
6: 28/01/2015 03:46:30 03:46:30 03:46:00      03:46
R>  str(DT,vec.len=2)
Classes ‘data.table’ and 'data.frame':  101 obs. of  4 variables:
 $ vardt     : chr  "28/01/2015 05:38:30" "27/01/2015 14:15:30" ...
 $ time      :Class 'ITime'  int [1:101] 20310 51330 21810 2250 64770 ...
 $ hour_min  :Class 'ITime'  int [1:101] 20280 51300 21780 2220 64740 ...
 $ c_hour_min: chr  "05:38" "14:15" ...
 - attr(*, ".internal.selfref")=<externalptr>

The first case, hour_min , preserves the ITime class, while the second case, c_hour_min , is just a character vector. 第一种情况, hour_min ,保留ITime类,而第二种情况, c_hour_min ,只是一个字符向量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM