0

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]))
  }
Pete
  • 15
  • 4
  • 1
    Why are you doing a loop at all? Why not just `index(time_series[1:3])`? Or what if you initialized `rtn` as a proper Date vector: `rtn <- rep(as.Date(NA), 3)` – MrFlick Oct 13 '14 at 06:36
  • I condensed what I am attempting. I need to return a list of things with the dates – Pete Oct 13 '14 at 06:43
  • @Pete you can't have a list of different types. Maybe you can store your date as a character. – agstudy Oct 13 '14 at 06:53
  • I reviewed that post and that would return a sequence of dates between the first and last date in my list. I only need the 3 dates returned. – Pete Oct 13 '14 at 07:05

1 Answers1

1

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"
Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295