2015-11-07 74 views
-1

enter image description here时间序列数据安排

我有几个时间序列数据,我想在进一步分析之前作出安排。重点是,正如你在图片中看到的那样,3个财务时间序列具有不同的日期 - 被观察到。如果至少有一个空白行,我想消除整行。为了做出安排,首先我将整个日期排除在左侧,除了2005年1月1日至2015年6月30日的星期六和星期日进行索引。

例如:在第11行,存在不匹配的日期。我想把NA列放在中间。

这里就是我试过

Day=data.frame(test[,1:2]) 
Rk=data.frame(test[,3:4]) 
Vix=data.frame(test[,5:6]) 
BA=data.frame(test[,7:8]) 

i=1 
k=0 
while(i<=2736){ 
if(Day[i,1]==Rk[i,1]){i=i+1} 
else if(Day[i,1]!=Rk[i,1]){ 
k=k+1 
Rk[i+1:k+2634,]=Rk[i:k+2633,] 
Rk[i,]=c(Day[i,1],NA) 
i=i+1} 

} 

,但它显示错误消息:更换不更换长度的倍数

我会非常感激的项目数量。任何形式的帮助都会比欢迎。

+0

我找到了解决这个问题的线索。如果您遇到困难,我建议您将NA列放在中间并使其循环。 – Song

+0

你应该说这是什么语言,否则没有人可以回答会看到它。 –

+0

对不起,我错过了。这是R码。 – Song

回答

0

如果使用像xts(或动物园)这样的时间序列类,这很容易。

# create sample data 
set.seed(21) 
dayone <- as.Date("2005-01-03") 
plusdays <-c(0:4, 7:11, 14:18, 21:24) 
test <- data.frame(date=dayone + plusdays) 
test$day <- weekdays(test$date, abbreviate=TRUE) 
test$date.1 <- dayone + c(plusdays[-11L], 25) 
test$Kernel <- rnorm(nrow(test), 3e-5, 1e-6) 
test$date.2 <- dayone + c(plusdays[-11L], 25) 
test$VIX.High <- round(rnorm(nrow(test), 14, 0.1), 2) 
test$date.3 <- dayone + c(plusdays[-11L], 25) 
test$Baa.Aaa <- round(rnorm(nrow(test), 0.66, 0.01), 2) 

require(xts) 
# create xts objects for each column 
Rk <- xts(test['Kernel'], test$date.1) 
Vix <- xts(test['VIX.High'], test$date.2) 
Ba <- xts(test['Baa.Aaa'], test$date.3) 
# use xts to merge data 
testxts <- merge(xts(,test$date), Rk, Vix, Ba) 

我已经离开了你day列,因为XTS /动物园对象只是一个索引属性矩阵,你不能在基质混合类型。但是您可以使用.indexwday函数来提取每行的工作日。