如何将毫秒秒刻度数据汇总到一分钟

时间:2018-02-28 10:26:53

标签: r

我有以下df,它位于刻度线上:

aud_tick <- data.frame(stringsAsFactors=FALSE,
                aud_time = c("2018-02-25 21:55:38.098", "2018-02-25 21:55:38.398",
                             "2018-02-25 21:55:41.099",
                             "2018-02-25 21:55:41.398", "2018-02-25 21:55:41.700",
                             "2018-02-25 21:55:42.000", "2018-02-25 21:55:57.308",
                             "2018-02-25 21:55:57.907", "2018-02-25 21:55:57.908",
                             "2018-02-25 21:56:17.418", "2018-02-25 21:56:17.420",
                             "2018-02-25 21:56:18.019", "2018-02-25 21:56:21.621",
                             "2018-02-25 21:56:21.624", "2018-02-25 21:56:47.888",
                             "2018-02-25 21:56:55.696", "2018-02-25 21:56:59.749",
                             "2018-02-25 21:56:59.750", "2018-02-25 21:57:00.349",
                             "2018-02-25 21:57:00.351"),
                aud_pair = c("AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD",
                             "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD",
                             "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD",
                             "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD", "AUD/USD",
                             "AUD/USD"),
                 aud_bid = c(0.78461, 0.78461, 0.78429, 0.78429, 0.78418, 0.78418, 0.78429,
                             0.78418, 0.78418, 0.78429, 0.78429, 0.78418,
                             0.78418, 0.78418, 0.78417, 0.78417, 0.78429, 0.78429,
                             0.78445, 0.78445),
                 aud_ask = c(0.78426, 0.78426, 0.78367, 0.78367, 0.78359, 0.78359, 0.78367,
                             0.78359, 0.78359, 0.78367, 0.78367, 0.78359,
                             0.78359, 0.78359, 0.7835, 0.7835, 0.78358, 0.78358,
                             0.78368, 0.78368)
            )

我想将数据汇总到一分钟内,在一分钟的窗口内显示这些细节:

  • aud_bid_high
  • aud_bid_low
  • aud_ask_higk
  • aud_ask_low

或者,任何人都知道如何将时间缩短到:

  • 最近10秒 - 21:55:38.098至21:55:40或21:55:30

我完全没有想法,我尝试了几种方法但无济于事。

有任何想法可以帮助我解决这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:0)

将数据格式化为适当的格式时,它会舍入到秒。你可以拆分并做任何你想要的聚合(平均值,最大值,最小值......)。

aud_tick$aud_time <- as.POSIXct(aud_tick$aud_time, format = "%Y-%m-%d %H:%M:%S")
split(aud_tick, f = aud_tick$aud_time)

$`2018-02-25 21:55:38`
             aud_time aud_pair aud_bid aud_ask
1 2018-02-25 21:55:38  AUD/USD 0.78461 0.78426
2 2018-02-25 21:55:38  AUD/USD 0.78461 0.78426

$`2018-02-25 21:55:41`
             aud_time aud_pair aud_bid aud_ask
3 2018-02-25 21:55:41  AUD/USD 0.78429 0.78367
4 2018-02-25 21:55:41  AUD/USD 0.78429 0.78367
5 2018-02-25 21:55:41  AUD/USD 0.78418 0.78359

$`2018-02-25 21:55:42`
             aud_time aud_pair aud_bid aud_ask
6 2018-02-25 21:55:42  AUD/USD 0.78418 0.78359

$`2018-02-25 21:55:57`
             aud_time aud_pair aud_bid aud_ask
7 2018-02-25 21:55:57  AUD/USD 0.78429 0.78367
8 2018-02-25 21:55:57  AUD/USD 0.78418 0.78359
9 2018-02-25 21:55:57  AUD/USD 0.78418 0.78359

$`2018-02-25 21:56:17`
              aud_time aud_pair aud_bid aud_ask
10 2018-02-25 21:56:17  AUD/USD 0.78429 0.78367
11 2018-02-25 21:56:17  AUD/USD 0.78429 0.78367

$`2018-02-25 21:56:18`
              aud_time aud_pair aud_bid aud_ask
12 2018-02-25 21:56:18  AUD/USD 0.78418 0.78359

$`2018-02-25 21:56:21`
              aud_time aud_pair aud_bid aud_ask
13 2018-02-25 21:56:21  AUD/USD 0.78418 0.78359
14 2018-02-25 21:56:21  AUD/USD 0.78418 0.78359

$`2018-02-25 21:56:47`
              aud_time aud_pair aud_bid aud_ask
15 2018-02-25 21:56:47  AUD/USD 0.78417  0.7835

$`2018-02-25 21:56:55`
              aud_time aud_pair aud_bid aud_ask
16 2018-02-25 21:56:55  AUD/USD 0.78417  0.7835

$`2018-02-25 21:56:59`
              aud_time aud_pair aud_bid aud_ask
17 2018-02-25 21:56:59  AUD/USD 0.78429 0.78358
18 2018-02-25 21:56:59  AUD/USD 0.78429 0.78358

$`2018-02-25 21:57:00`
              aud_time aud_pair aud_bid aud_ask
19 2018-02-25 21:57:00  AUD/USD 0.78445 0.78368
20 2018-02-25 21:57:00  AUD/USD 0.78445 0.78368

答案 1 :(得分:0)

我用过: lubridate::floor_date

有多种选项可用于舍入时间刻度,从秒到一年