Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于日期';n';显示在data.frame rownames中后的行_R_Sorting_Indexing_Xts_Rowname - Fatal编程技术网

基于日期';n';显示在data.frame rownames中后的行

基于日期';n';显示在data.frame rownames中后的行,r,sorting,indexing,xts,rowname,R,Sorting,Indexing,Xts,Rowname,我感谢akrun提供了下面的代码。它为我的问题提供了部分解决方案 library(xts) data(sample_matrix) sample.xts <- as.xts(sample_matrix, descr='my new xts object') start_date <- '2007-01-02' end_date <- '2007-01-05' col_names <- c('Open', 'High') 我真正需要的是开始日期+5和结束日期+5之间的

我感谢akrun提供了下面的代码。它为我的问题提供了部分解决方案

library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')

start_date <- '2007-01-02'
end_date <- '2007-01-05' 
col_names <- c('Open', 'High')
我真正需要的是
开始日期+5
结束日期+5
之间的值

 sample.xts[paste(as.Date(start_date) + 5, as.Date(end_date) + 5, sep="/"), col_names]

问题是我有交易日,因此不仅不包括周末,也不包括假期等。例如,在某些情况下+5只产生3个值,因为其他2个是周末。因此,我想将上述任意日历日设置为
as.Date
格式。我需要该表中接下来的5个值,而不是接下来的5个日历日

我想这就是你想要的:

first(sample.xts[paste0(start_date,'/')], 5)

根据上面的代码,预期输出是什么。请实际运行代码并提供实际输出<代码>数据(sample.xts)会导致错误,在上一个问题中,您将打印字符向量的“输出”粘贴为
[1]“Stock1”、“Stock2”、“Stock3”
,但R不会在输出中加逗号。2007-01-06和2007-01-10之间的“开放”和“高”价格。但请注意,样本数据有日历日,不能代表实际财务数据的天数。这包括周末和节假日。刚刚更新。忘了几个lines@JoshuaUlrich今天,我的电脑上的R控制台无法上网,所以我一直在输入问题中的大部分代码。好样的!!
first(sample.xts[paste0(start_date,'/')], 5)