按时间分组数据

时间:2013-04-10 05:31:16

标签: frames

我有一个名为example.csv的文件,其中包含以下数据:

day,number,price
2010-01-01 00:01:00,1,0.4
2010-01-01 00:02:00,2,1.2
2010-01-01 00:03:00,3,2.5
2010-01-01 00:04:00,4,9.1
2010-01-01 00:05:00,5,3.4
2010-01-01 00:06:00,6,6.9
2010-01-01 00:07:00,7,8.9
2010-01-01 00:08:00,8,9.1
2010-01-01 00:09:00,9,4.2
2010-01-01 00:10:00,10,11.2
2010-01-01 00:11:00,11,53.12
2010-01-01 00:12:00,12,45.21
2010-01-01 00:12:00,13,1.1
2010-01-01 00:13:00,14,3.43
2010-01-01 00:14:00,15,21.42

加载文件内容:

example = read.csv(file="path/example.csv", sep=",")

加载

DD <- structure(list(day = structure(c(example$day), class = c("POSIXct", "POSIXt"),   tzone = ""), 
 number = c(example$number), price = c(example$price)), .Names = c("day", "number", "price"), row.names = c(NA, 
-15L), class = "data.frame")

之后:

ddx <- xts(DD[,c('number','price')], order.by = DD[,'day'])

和:     period.apply(ddx,endpoints(ddx,on ='minutes',k = 3),sum)

在最后一段时间之后。应用它回复了这个:

                       [,1]
  1970-01-01 02:00:14 301.18

为什么回馈1970年

1 个答案:

答案 0 :(得分:1)

Datetime列作为POSIXct列的其他日期时间类后,您可以使用period.apply中的xts。要获得3分钟间隔,请使用endpoints(dd.xts, on = 'minutes', k = 3)

# with a data frame
DD <- structure(list(Dates = structure(c(1034644620, 1034644800, 1034644920, 
1034734860, 1034734920), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Price = c(0.6, 1.4, 4.1, 1.6, 7.7), Price.2 = c(5, 2.4, 9.1, 
    1.4, 3.7)), .Names = c("Dates", "Price", "Price.2"), row.names = c(NA, 
-5L), class = "data.frame")
DD 

#                Dates Price Price.2
# 1 2002-10-15 11:17:00   0.6     5.0
# 2 2002-10-15 11:20:00   1.4     2.4
# 3 2002-10-15 11:22:00   4.1     9.1
# 4 2002-10-16 12:21:00   1.6     1.4
# 5 2002-10-16 12:22:00   7.7     3.7
# coerce to xts

ddx <- xts(DD[,c('Price','Price.2')], order.by = DD[,'Dates'])

period.apply(ddx, endpoints(ddx, on = 'minutes',k=3), sum)


##                      [,1]
## 2002-10-15 11:17:00  5.6
## 2002-10-15 11:20:00  3.8
## 2002-10-15 11:22:00 13.2
## 2002-10-16 12:22:00 14.4