- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
申请 split
函数到 xts
对象来自 weeks
将行分组为每周块。组中的默认天数是 Monday
至 Sunday
.想要群里的天数来自Sunday
怎么办?至 Saturday
?
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
最佳答案
我经常在周日而不是周一按周拆分,因为我处理外汇数据(市场在纽约东部时间周日下午开盘)。这是一个有效的解决方案,split_FXweeks
,使用“xts方式”分割时间序列数据。当您长时间处理高密度刻度数据时,这种方法非常快。
此技巧的功劳归功于以下链接中的技巧 1: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
关于r - xts 按周拆分函数将一周的第一天指定为星期日而不是默认的星期一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49603445/
我如何使用 apply 系列函数,比如 apply.daily到多元 XTS? 例如: 时间,a,b ... 2012-02-11 16:21:24 4.7258 7.7258 2012-02-11
我有一个 xts 对象列表,这些对象具有共同的索引和列名。 我想按索引进行 rbind 并对列进行平均: dts = seq.POSIXt(from = Sys.time() - days(2), t
我正在使用 xts 时间序列在 R 中工作。 我有什么: 具有不等间隔时间步长的时间序列数据集。 我想得到什么: 具有等距时间步长的时间序列,其值对应于与时间步长重叠的原始值的比例(请参见下面的示例)
this = structure(c(-0.012, -0.028, -0.044, -0.033, -0.039, -0.042), .Dim = c(3L, 2L), .Dimnames
在 xts 对象中是否有一种方法可以执行与下面相同的操作,但对于具有多天盘中数据的 xts 对象?下面的工作就像一个时钟,但一天的数据。如果我从 22 日到 26 日通过 xts,它不会。似乎不可能一
library(xts) set.seed(1) x = xts( cbind(a=1:10,b=20:11) , Sys.Date()+1:10 ) y = xts( runif(10) , Sys
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb')) source(con) close(con) load.pack
我有一个 list 。列表由 5 个 xts 对象组成。每个对象由 5 个 xts 系列组成。每个系列包含 183 个观察值。我想从所有这些系列中提取一个公共(public)系列。我要减去的系列名为
在将 rowSums 传递给 xts 对象时,是否可以保留 xts 对象的索引? 目前,我将结果重新转换为 xts 对象,但是如果 rowSums 能够简单地返回它的内容,这似乎并没有那么快已通过。
首先让我说一下,我看了一下 ?xts,意识到这是一个与时区相关的问题,似乎已经解决了,但我不明白为什么 它正在发生。所以:我有一个简单的价格数据数据框。当我将它转换为 xts 对象时,xts 对象的第
当我尝试执行以下工作时发生错误: # generate random integrals # data <- xts(floor(runif(100, 1,101)),as.Date("1973-02
这是输出: library(tseries) # for adf.test function adf.test(data) Augmented Dickey-Fuller Test data: da
如何删除 xts 图右上角的日期范围?例如,在下面 xts 图的右上角,我想删除文本“2007-01-02/2007-06-30”。 library(xts) data(sample_matrix)
我有一个不规则的时间序列并且正在使用 xts的 endpoints 获取我的时间序列的每小时索引。 endpoints(data, on="hours") 我正在使用它以这种方式计算每小时 perio
申请 split函数到 xts对象来自 weeks将行分组为每周块。组中的默认天数是 Monday至 Sunday .想要群里的天数来自Sunday怎么办?至 Saturday ? library(x
我在删除 xts 对象中的重复行时遇到问题。我有一个 R 脚本,它将下载货币的刻度财务数据并将其转换为 OHLC 格式的 xts 对象。该脚本还会每 15 分钟提取一次新数据。新数据从今天的第一笔交易
我真的很难在这里找到解决方案。如果您查看最后一行代码,您会明白我想在下次开盘时买入并在 5 天后卖出 (x = 5)。 问题是 xts 索引正在计算周末。因此,例如,您会在 7 月 12 日星期五获得
我有 3 个 xts 对象,它们的标记都是“日期”对象: > a a 1995-01-03 1.76 1995-01-04 1.69
我正在尝试按天比较不同的时间序列。 目前典型的 XTS 对象如下所示: > vwap.crs QUANTITY QUANTITY.1 2014-03-03 13
我在列表中有一些数据,如下所示: > y $ABMD.Rank ABMD.Rank $ATVI.Rank ATVI.Rank $ADBE.Rank ADBE.Rank $
我是一名优秀的程序员,十分优秀!