2

我有一个名为 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)

在最后一个 period.apply 之后,它会返回:

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

为什么它还给1970

4

1 回答 1

1

Datetime列作为POSIXct其他日期时间类后,您可以使用period.applyfrom 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
于 2013-04-10T06:19:16.933 回答