汇总股票价格数据的间隔为5分钟

时间:2018-05-30 01:03:27

标签: r datetime dplyr

我想汇总股票价格数据的5分钟间隔:这是我的数据。

Time Price 
2/21/2018 09:00:00am 122.1 
2/21/2018 09:07:38am 122.4 
2/21/2018 09:09:10am 122.4 
2/21/2018 09:09:10am 122.4 
2/21/2018 09:09:21am 122.2 
2/21/2018 09:13:16am 122.3 
2/21/2018 09:13:16am 122.3 
2/21/2018 09:13:16am 122.3 
2/21/2018 09:13:16am 122.3 
2/21/2018 09:14:09am 122.3 
2/21/2018 09:14:09am 122.3 
2/21/2018 09:14:09am 122.3 
2/21/2018 09:15:06am 122.4 
2/21/2018 09:15:06am 122.4 
2/21/2018 09:15:06am 122.4 
2/21/2018 09:15:06am 122.4 
2/21/2018 09:15:45am 122.4 
2/21/2018 09:19:32am 122.5 
2/21/2018 09:19:32am 122.5

这是我在r:

中的编码
## Coding for date and time format
nestle_2days$Time <- as.POSIXct(nestle_2days$Time, format="%m/%d/%Y %H:%M")
## Aggregate data for 5 minutes interval
require(dplyr) 
library(padr)
nestle_2days %>% 
    group_by(Time = cut(Time, breaks="5 min")) %>% summarise(dprice = mean(Price)) %>% 
    print(nestle_2days)

输出是:

Time dprice 
1 2018-02-21 02:30:00 123. 
2 2018-02-21 02:35:00 123. 
3 2018-02-21 02:40:00 123. 
4 2018-02-21 02:45:00 123. 
5 2018-02-21 02:50:00 123. 
6 2018-02-21 02:55:00 123. 
7 2018-02-21 03:00:00 123. 
8 2018-02-21 03:05:00 123. 
9 2018-02-21 03:10:00 123. 
10 2018-02-21 03:15:00 123.
... with 124 more rows

为什么时间从2018-02-21 02:30:00开始,为什么不在09:00:00

请帮帮我:提前谢谢

1 个答案:

答案 0 :(得分:0)

您的Time列似乎无法正确解析 使用mdy_hms() lubridate,您的代码可以正确执行:

library(dplyr)
library(lubridate)

nestle_2days %>%
  mutate(Time = mdy_hms(Time)) %>%
  group_by(Time = cut(Time, breaks="5 min")) %>% 
  summarise(dprice = mean(Price)) 

                 Time  dprice
1 2018-02-21 09:00:00 122.100
2 2018-02-21 09:05:00 122.350
3 2018-02-21 09:10:00 122.300
4 2018-02-21 09:15:00 122.429