R中的聚合函数不能与POSIXct一起使用

时间:2015-01-10 06:27:07

标签: r

我想汇总我的数据。

time<- as.POSIXct(c("2014-12-10 00:11:02","2014-12-10 00:11:02","2014-12-10 00:11:03"), tz= "GMT")
shares<-c(1,2,3)
price<-c(48, 48, 49)
dat<-data.frame(time= as.factor(time), price= price, size =shares)
dat

这是dat的样子:

             time price size
1 2014-12-10 00:11:02    48    1
2 2014-12-10 00:11:02    48    2
3 2014-12-10 00:11:03    49    3

现在我尝试聚合dat

aggregate(x=dat, by = list( as.factor(dat$size)), formula = cbind(dat$time, dat$price) ~ as.numeric(dat$size), FUN="sum")

我想在时间和价格中汇总dat中的份额列,所以它看起来像这样:

time                  price  size
2014-12-10 00:11:02    48    3
2014-12-10 00:11:03    49    3

任何想法?谢谢。

1 个答案:

答案 0 :(得分:3)

无论

aggregate(x = dat$size, by = list(time, price), FUN = "sum")

              Group.1 Group.2 x
1 2014-12-10 00:11:02      48 3
2 2014-12-10 00:11:03      49 3

或者

aggregate(size ~ time + price, data = dat, FUN = "sum")

                 time price size
1 2014-12-10 00:11:02    48    3
2 2014-12-10 00:11:03    49    3

这些对应于?aggregate的文档,它们提供了可能的表单:

aggregate(x, by, FUN, ..., simplify = TRUE)
aggregate(formula, data, FUN, ..., subset, na.action = na.omit)

您的尝试将这些表单混合在一起。