Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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
如何在R中延迟时间序列中的日期索引?_R_Date_Xts_Lag_S - Fatal编程技术网

如何在R中延迟时间序列中的日期索引?

如何在R中延迟时间序列中的日期索引?,r,date,xts,lag,s,R,Date,Xts,Lag,S,改写问题如下: 我已经取得了一些进步,但是我从R 这是我开始的xts <no title> Value Value2 Value3 2002-08-21 21 2 27 2003-09-10 22 42 87 2004-02-12 23 62 67 2005-04-13 24 13 73 2006-05-13 25 4 28 2007-08-14 20

改写问题如下:

我已经取得了一些进步,但是我从R

这是我开始的xts

<no title>  Value   Value2  Value3
2002-08-21  21      2       27
2003-09-10  22      42      87
2004-02-12  23      62      67
2005-04-13  24      13      73
2006-05-13  25      4       28
2007-08-14  20      68      25
2008-03-06  19      82      22
所以我有点糊涂了

原问题如下:

我在R中有一个时间序列(我学得很快,但显然还不够快!),就像这样

             Value
2002-08-21    21
2003-09-10    22
2004-02-12    23
2005-04-13    24
2006-05-13    25
2007-08-14    20
2008-03-06    19
我想在每一行的新列的下一行创建一个日期索引的派生:

              Value    NextDate
2002-08-21    21       2003-09-10
2003-09-10    22       2004-02-12
2004-02-12    23       2005-04-13
2005-04-13    24       2006-05-13
2006-05-13    25       2007-08-14
2007-08-14    20       2008-03-06
2008-03-06    19       [...]
对于Value(使用Lag)来说,这非常容易,但是对于日期索引本身就不容易了

我可能可以通过各种查找之类的方法来解决这个问题,但这很麻烦。您必须在其他字段上进行匹配,或者摆弄感觉不太“符合R”的行号

有没有一种漂亮、整洁、优雅的方式

我敢肯定,只要有人回答我,我就会说“噢!”!但到目前为止,我还没有在这个网站上找到一个滞后于日期索引的答案


我想这样做的原因是我想使用一行中的每一对日期来询问另一个系列。因此,可能有更好的方法来实现这一点。

我不确定
xts
对于您的尝试来说是最好的,但这里的价值在于如何获取
xts
对象,创建一个
dataframe
并创建所需的额外时间列,然后将其转换为时间格式

 data(sample_matrix)
 x <- as.xts(sample_matrix)
 head(x)
 df <-as.data.frame(x)
 head(df)
 newdates<-rownames(df)

 df$nextdates<-c(newdates[2:length(newdates)],"NA")
 df$nextdates<-as.POSIXct(strptime(df$nextdates, "%Y-%m-%d"))
 head(df)
数据(样本矩阵)

x我认为这与您实际想要做的事情类似:

library(xts)
#create example xts
times <- seq(as.Date('2002-08-21'),as.Date('2002-09-06 '),by="day")
myts <- xts(x=1:length(times),order.by=times)

#second xts, with start and end times
times2 <- c("2002-08-21","2002-08-31","2002-09-06")    
myts2 <- myts[times2] 

#get start and end times
ix <- index(myts2)

#get positions in myts
ep <- which(index(myts) %in% ix)-1

#calculate means
period.apply(myts,ep,mean) 
库(xts)
#创建示例xts

时代我相信你在寻找的是:

dayDifff <- function(X)
{
    as.numeric(as.Date(index(X))) - c(NA, as.numeric(as.Date(index(X[-nrow(X)]))))
}

dayDifff您在R中的实际对象是什么类?使用xts-对不起,应该这么说!你说的“审问另一个系列”到底是什么意思?@BitRocker:我会重新考虑你想做什么。我的首选是使用
merge(X,lag(X))
,这对于
xts
来说既便宜又快速。如果您真的选择了下一个额外的日期列(为什么?),请切换到使用data.frame并删除xts。你的电话。@BitRocker:至于你的滑动平均线,
zoo
xts
已经帮你做了。阅读动物园的小插曲以获得灵感。哇,太棒了。现在我需要弄清楚它是如何工作的。。。谢谢@user1317221_G当然我还在考虑它-我没有像您的示例中那样使用文本表,所以我重新将其用于xts的实际日期索引。我使用index()访问它。这会在df$dates[df$dates[2]:位上抛出一个错误,我将其替换为df$dates([2]如果您显示
dput(yourTS)的输出,我要做的就是thisDate
我相信你会得到一个适合你情况的答案。但是,我真的建议你展示你的整个问题,而不仅仅是你认为你陷入困境的那一步。可能有更好的方法来实现你的目标。好的,各位,我也在这里学习礼仪……重新写下原始问题嗯,这很有趣。让我来看看好好想想。这个答案启发了我去创造正确的解决方案。很多人都提供了帮助,但这绝对是让我达到目的的原因。除非有人反对,否则我会给它打分。
              Value    NextDate
2002-08-21    21       2003-09-10
2003-09-10    22       2004-02-12
2004-02-12    23       2005-04-13
2005-04-13    24       2006-05-13
2006-05-13    25       2007-08-14
2007-08-14    20       2008-03-06
2008-03-06    19       [...]
 data(sample_matrix)
 x <- as.xts(sample_matrix)
 head(x)
 df <-as.data.frame(x)
 head(df)
 newdates<-rownames(df)

 df$nextdates<-c(newdates[2:length(newdates)],"NA")
 df$nextdates<-as.POSIXct(strptime(df$nextdates, "%Y-%m-%d"))
 head(df)
library(xts)
#create example xts
times <- seq(as.Date('2002-08-21'),as.Date('2002-09-06 '),by="day")
myts <- xts(x=1:length(times),order.by=times)

#second xts, with start and end times
times2 <- c("2002-08-21","2002-08-31","2002-09-06")    
myts2 <- myts[times2] 

#get start and end times
ix <- index(myts2)

#get positions in myts
ep <- which(index(myts) %in% ix)-1

#calculate means
period.apply(myts,ep,mean) 
dayDifff <- function(X)
{
    as.numeric(as.Date(index(X))) - c(NA, as.numeric(as.Date(index(X[-nrow(X)]))))
}