R data.table: Selecting minimum value of chron within group

677 views Asked by At

For each group in a data.table, I want to repeat the value of the minimum (earliest) timestamp. Consider the following data:

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

The following runs, but when I try to view the data.table, I get an error:

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 

Session info: R version 3.1.1, chron_2.3-45, data.table_1.9.2

2

There are 2 answers

2
DatamineR On BEST ANSWER

Do you mean it like this?

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)
0
brorgschlr On

The following does what I want, though I'd prefer assignment by reference as attempted in my question (e.g., I have to rename columns).

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)