xts split by week 函数将一周的第一天指定为星期日而不是默认的星期一

xts split by week function specify first day of week as Sunday instead of default of Monday

通过 weekssplit 函数应用于 xts 对象将行分组为每周块。组中的默认天数是 MondaySunday。我想让群里的天数从SundaySaturday怎么办?

library(xts)
idx <- as.Date("2018-3-1") + 0:14
v <- 1:15
x <- xts(v, idx)
group <- split(x, f = 'weeks')
group

Output:
[[1]]
           [,1]
2018-03-01    1  # Thursday
2018-03-02    2  # Friday
2018-03-03    3  # Saturday
2018-03-04    4  # Sunday

[[2]]
           [,1]
2018-03-05    5  # Monday
2018-03-06    6  # Tuesday
2018-03-07    7  # Wednesday
2018-03-08    8  # Thursday
2018-03-09    9  # Friday
2018-03-10   10  # Saturday
2018-03-11   11  # Sunday

[[3]]
           [,1]
2018-03-12   12  # Monday
2018-03-13   13  # Tuesday
2018-03-14   14  # Wednesday
2018-03-15   15  # Thursday

Desired Output:
[[1]]
           [,1]
2018-03-01    1  # Thursday
2018-03-02    2  # Friday
2018-03-03    3  # Saturday

[[2]]
           [,1]
2018-03-04    4  # Sunday
2018-03-05    5  # Monday
2018-03-06    6  # Tuesday
2018-03-07    7  # Wednesday
2018-03-08    8  # Thursday
2018-03-09    9  # Friday
2018-03-10   10  # Saturday

[[3]]
           [,1]
2018-03-11   11  # Sunday
2018-03-12   12  # Monday
2018-03-13   13  # Tuesday
2018-03-14   14  # Wednesday
2018-03-15   15  # Thursday

考虑创建一个外部 equal-length 周数 的矢量,使用 %U 格式从周日开始的工作日。参见 ?strftime

%U

Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.

week_num <- format(idx, "%U")
group <- unname(split(x, f = week_num))
group

[[1]]

2018-03-01 1
2018-03-02 2
2018-03-03 3

[[2]]

2018-03-04  4
2018-03-05  5
2018-03-06  6
2018-03-07  7
2018-03-08  8
2018-03-09  9
2018-03-10 10

[[3]]

2018-03-11 11
2018-03-12 12
2018-03-13 13
2018-03-14 14
2018-03-15 15

我经常在星期日而不是星期一按周划分,因为我处理外汇数据(市场在纽约东部时间周日下午开放)。这是一个有效的解决方案,split_FXweeks,使用拆分时间序列数据的"xts way"。当您长时间处理高密度刻度数据时,这种方法非常快。

此技巧归功于下面的技巧 1 link:http://darrendev.blogspot.com.au/2012/08/small-rxts-code-snippets-and-tips.html

添加了与其他建议方法相比的基准作为基准。

idx <- as.Date("2018-3-1") + 0:14
v <- 1:15
x <- xts(v, idx)


split_FXweeks <- function(x) {
  ep <- .Call("endpoints", .index(x) + 4L * 86400L, 604800L, 
              1, TRUE, PACKAGE = "xts")
  sp <- (ep + 1)[-length(ep)]
  ep <- ep[-1]
  lapply(1:length(ep), function(X) x[sp[X]:ep[X]])
}


split1 <- function(idx, x) {
  week_num <- format(idx, "%U")
  group <- unname(split(x, f = week_num))
  group
}

library(microbenchmark)
microbenchmark(
  y <- split_FXweeks(x),
  z <- split1(idx, x))
# Unit: microseconds
# expr     min      lq      mean   median       uq     max neval
# y <- split_FXweeks(x)  52.521  60.167  72.90766  75.2390  80.6495 162.077   100
# z <- split1(idx, x) 325.681 351.658 383.13293 364.2215 384.9765 881.486   100
# > y
# [[1]]
# [,1]
# 2018-03-01    1
# 2018-03-02    2
# 2018-03-03    3
# 
# [[2]]
# [,1]
# 2018-03-04    4
# 2018-03-05    5
# 2018-03-06    6
# 2018-03-07    7
# 2018-03-08    8
# 2018-03-09    9
# 2018-03-10   10
# 
# [[3]]
# [,1]
# 2018-03-11   11
# 2018-03-12   12
# 2018-03-13   13
# 2018-03-14   14
# 2018-03-15   15