0

您好,我正在寻找“min​​ datetime by group”的等价物plyr(或任何其他包,但 data.table 导致bug #2531的原因)

set.seed(1)
datetimes=c(rep(as.POSIXct("2011-01-01 08:00:00"),3) , rep(as.POSIXct("2011-01-03 08:00:00"),3)) + rnorm(6)
groups=c(rep('a',3),rep('b',3))
X = data.frame(datetime=datetimes,group=groups)
X
#                  datetime group
#2011-01-01 07:59:59.373546     a
#2011-01-01 08:00:00.183643     a
#2011-01-01 07:59:59.164371     a
#2011-01-03 08:00:01.595280     b
#2011-01-03 08:00:00.329507     b
#2011-01-03 07:59:59.179531     b

预期答案

                    datetime group
2011-01-01 07:59:59.164371     a
2011-01-03 07:59:59.179531     b
4

2 回答 2

3

试试这个:

aggregate(datetime ~ group, X, min)
于 2013-04-02T15:09:14.633 回答
1

使用ddply.

ddply(X, .(group), summarise, first_datetime = min(datetime))

这个变体更简洁,但不是很清楚:

ddply(X, .(group), colwise(min))
于 2013-04-02T14:18:41.233 回答