Reputation: 15
I am trying to return a time series that is of class "xts" "zoo". I would like to retrieve times but instead get numbers. I have an example below:
rtn<-c(rep(NA,3))
for(i in 1:3){
rtn[i]<-index(time_series[i])
}
This returns:
[1] 13704 14049 14343
This is what I would like it to return:
[1] "2007-07-10" "2008-06-19" "2009-04-09"
Thank you in advance for the help.
Above is a simplified version. It is the only part of the code that I can't get to run. If it is helpful here is the actual code:
green_rtn<-c(rep(NA,length(green_series_open[,1])))
for(i in 1:length(green_series_open[,1])){
green_rtn[i]<-straddles(coredata(green_series_open[i,1]),coredata(green_series_open[i,2]),
index(green_series_open[i]),index(green_series_close[i]))
}
Upvotes: 0
Views: 54
Reputation: 206263
You should properly initialize rtn
as a Date vector. Using @akrun's sample data (it would have been nice had you included your own reproducible example)
rtn <- rep(as.Date(NA), 3)
library(xts)
time_series <- xts(rnorm(5), order.by=as.Date(c('2007-07-10', '2008-06-19', '2009-04-09', '2009-05-06', '2009-05-08')))
for(i in 1:3) {
rtn[i]<-index(time_series[i])
}
rtn
# [1] "2007-07-10" "2008-06-19" "2009-04-09"
Upvotes: 1