R data.table:选择组内chron的最小值

时间:2014-12-03 02:25:27

标签: r data.table chron

对于data.table中的每个组,我想重复最小(最早)时间戳的值。请考虑以下数据:

library(chron)
library(data.table)
set.seed(12349870)
time.stamp<-chron(c(10000.673,sample(10001:20000,9)))
group<-c(rep(1,5),rep(2,5))
timedata<-data.table(time.stamp=time.stamp,group=group)
timedata

#   1: (05/19/97 16:09:07)     1
#   2: (03/02/21 00:00:00)     1
#   3: (02/20/15 00:00:00)     1
#   4: (12/11/10 00:00:00)     1
#   5: (08/23/10 00:00:00)     1
#   6: (07/22/18 00:00:00)     2
#   7: (06/09/23 00:00:00)     2
#   8: (03/02/13 00:00:00)     2
#   9: (06/04/09 00:00:00)     2
#   10: (12/04/12 00:00:00)     2

以下运行,但是当我尝试查看data.table时,出现错误:

timedata[,firstdata:=time.stamp[which.min(time.stamp)],by=group]
timedata
#Error in format.dates(x, format[[1]], origin. = origin., simplify = simplify) :
#unknown date format 

会话信息:R版本3.1.1,chron_2.3-45,data.table_1.9.2

2 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

stopifnot(sessionInfo()$otherPkgs$data.table$Version=="1.9.4")
timedata[,firstdata:=time.stamp[which.min(time.stamp)],by=group]
timedata
#            time.stamp group           firstdata
#1: (05/19/97 16:09:07)     1 (05/19/97 16:09:07)
#2: (03/02/21 00:00:00)     1 (05/19/97 16:09:07)
#3: (02/20/15 00:00:00)     1 (05/19/97 16:09:07)
#4: (12/11/10 00:00:00)     1 (05/19/97 16:09:07)
#5: (08/23/10 00:00:00)     1 (05/19/97 16:09:07)
#6: (07/22/18 00:00:00)     2 (06/04/09 00:00:00)
#7: (06/09/23 00:00:00)     2 (06/04/09 00:00:00)
#8: (03/02/13 00:00:00)     2 (06/04/09 00:00:00)
#9: (06/04/09 00:00:00)     2 (06/04/09 00:00:00)
#10:(12/04/12 00:00:00)     2 (06/04/09 00:00:00)

答案 1 :(得分:0)

以下是我想要的,虽然我更喜欢在我的问题中尝试按引用分配(例如,我必须重命名列)。

setkey(timedata,group,time.stamp)
timedata<-timedata[timedata[,.SD[1],keyby=group]]

changename<-function(dt,oldname,newname){
   nm<-names(dt)
   pos<-which(nm==oldname)
   stopifnot(length(pos)>0)
   nm[pos]<-newname
   setnames(dt,names(dt),nm)
}

changename(timedata,"time.stamp.1","firstdata")
timedata

#    group         time.stamp           firstdata
#1:     1 (05/19/97 16:09:07) (05/19/97 16:09:07)
#2:     1 (08/23/10 00:00:00) (05/19/97 16:09:07)
#3:     1 (12/11/10 00:00:00) (05/19/97 16:09:07)
#4:     1 (02/20/15 00:00:00) (05/19/97 16:09:07)
#5:     1 (03/02/21 00:00:00) (05/19/97 16:09:07)
#6:     2 (06/04/09 00:00:00) (06/04/09 00:00:00)
#7:     2 (12/04/12 00:00:00) (06/04/09 00:00:00)
#8:     2 (03/02/13 00:00:00) (06/04/09 00:00:00)
#9:     2 (07/22/18 00:00:00) (06/04/09 00:00:00)
#10:    2 (06/09/23 00:00:00) (06/04/09 00:00:00)