3
votes

I am new to coding in R and have found myself stuck. My file consists of highfrequency stock index data and it's converted into an xts file. See below (Left column is date and time and right column is intraday return):

2007-01-02 09:00:00  0.000000e+00

2007-01-02 09:01:00  2.670405e-05

2007-01-02 09:02:00  1.387629e-03

2007-01-02 09:03:00 -1.866841e-04

2007-01-02 09:04:00 -3.201110e-04

2007-01-03 09:00:00 0,0000000000

2007-01-03 09:01:00 -1.321929e-04

2007-01-03 09:02:00 -1.586546e-04

2007-01-03 09:03:00  7.665975e-04

2007-01-03 09:04:00 -5.284993e-05

2007-01-03 09:05:00 -2.907246e-04

My data goes over five years and what I want to is subset it by 500 days and then move it forward 30 days in a loop. However, the loop in itself is not needed, but help of how to construct this kind of function would be highly appreciated.

I have used following function, in which my dataset is named "DIRV" that is the sum of intraday returns:

b<-head(DIRV, n=500)

L<-as.Date(index(b)[1])

J<-as.Date(index(b)[500])

V<-IRV["L/J"]

But I get the following error:

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year, : missing value where TRUE/FALSE needed

In addition: Warning messages:

1: In as_numeric(YYYY) : NAs introduced by coercion 2: In as_numeric(YYYY) : NAs introduced by coercion

1

1 Answers

1
votes

Your error arises because you are trying to subset over the date-times being the character string "L/J" which makes no sense to the subsetting operations. Also, you should be careful that L or J do not return NA, which can occur on the boundaries of your data, otherwise you'll get errors.

Try this instead to avoid the error:

if (is.na(L) || is.na(J)) stop("Not a valid date range.")
V <-IRV[paste0(L,"/", J), ]

You want to carefully handle the edge cases with your window.

I don't quite follow what you mean when you say "what I want to is subset it by 500 days and then move it forward 30 days in a loop". You might be able to use rollapplyr (see ?rollapplyr) if you mean computing a rolling window of data.