2

我在使用时DateIDate在使用时分配一个新列by =。它正在创建一个整数列,而不是Date预期的。

require(data.table)
dt <- data.table(date = as.IDate(sample(10000:11000, 10), 
                                 origin = "1970-01-01"))
dt[, group := rep(1:2, 5)]
print(dt)

#           date group
#  1: 1997-06-12     1
#  2: 1998-02-19     2
#  3: 1998-04-25     1
#  4: 1998-01-27     2
#  5: 1997-10-29     1
#  6: 1998-05-08     2
#  7: 1999-05-09     1
#  8: 1999-06-26     2
#  9: 1997-11-01     1
# 10: 1997-07-19     2

这有效:

dt[, min.date := min(date)]
print(dt)

#           date group   min.date
#  1: 1997-06-12     1 1997-06-12
#  2: 1998-02-19     2 1997-06-12
#  3: 1998-04-25     1 1997-06-12
#  4: 1998-01-27     2 1997-06-12
#  5: 1997-10-29     1 1997-06-12
#  6: 1998-05-08     2 1997-06-12
#  7: 1999-05-09     1 1997-06-12
#  8: 1999-06-26     2 1997-06-12
#  9: 1997-11-01     1 1997-06-12
# 10: 1997-07-19     2 1997-06-12

但问题是:

dt[, min.group.date := as.IDate(min(date)), by = group]
print(dt)

#           date group   min.date min.group.date
#  1: 1997-06-12     1 1997-06-12          10024
#  2: 1998-02-19     2 1997-06-12          10061
#  3: 1998-04-25     1 1997-06-12          10024
#  4: 1998-01-27     2 1997-06-12          10061
#  5: 1997-10-29     1 1997-06-12          10024
#  6: 1998-05-08     2 1997-06-12          10061
#  7: 1999-05-09     1 1997-06-12          10024
#  8: 1999-06-26     2 1997-06-12          10061
#  9: 1997-11-01     1 1997-06-12          10024
# 10: 1997-07-19     2 1997-06-12          10061

min.group.date是数字而不是Date.

dt[, class(min.group.date)]

# [1] "numeric"

如果我将列初始化为Dateor IDate,它将按预期工作:

dt <- data.table(date = as.IDate(sample(10000:11000, 10), origin = "1970-01-01"))
dt[, group := rep(1:2, 5)]

dt[, min.group.date := as.IDate(NA)]
dt[, min.group.date := min(date), by = group]

dt[, class(min.group.date)]
# [1] "IDate" "Date"
4

1 回答 1

1

保罗,如果你想要的只是按最少日期分组,这行就可以了:

dt[,min(date),by=group]

您应该看到(由于示例中的“示例”命令,下面的日期显然与您的日期不同):

   group         V1
1:     1 1997-11-19
2:     2 1997-12-04

如果您想查看每一行,您可以加入表格:

setkey(dt,group) #always good practice
dt_min=dt[,min(date),by=group]
setnames(dt_min,"V1","min.group.Date") #you should NOT use colnames (see help('setnames')
dt[dt_min]


    group       date min.group.Date
 1:     1 1999-01-30     1997-11-19
 2:     1 1999-11-27     1997-11-19
 3:     1 1999-11-11     1997-11-19
 4:     1 1997-11-19     1997-11-19
 5:     1 1999-05-06     1997-11-19
 6:     2 1999-07-11     1997-12-04
 7:     2 1997-12-04     1997-12-04
 8:     2 1998-07-28     1997-12-04
 9:     2 1998-10-23     1997-12-04
10:     2 1998-06-01     1997-12-04
于 2013-04-14T10:34:36.143 回答