R function: window ()

I have a matrix called x that looks like this:

  pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2265 4 1275264600 1.2268 5 1275264900 1.2265 6 1275265200 1.2265 7 1275265500 1.2270 8 1275265800 1.2269 9 1275266100 1.2268 10 1275266400 1.2275 ...1000 rows 

I converted it to a time series data type ( mts[2000] ) using tser<- ts(x)1

Now I want to use the window() function (from the stats package) to isolate all the lines between # 5 and # 8 based on their POSIX timestamp (pTime field), but I get an error.

 > A<- as.POSIXct(tser[5,1],origin="1970-01-01 00:00:00 UTC") > B<- as.POSIXct(tser[8,1],origin="1970-01-01 00:00:00 UTC") > A pTime "2010-05-31 01:15:00 EDT" > B pTime "2010-05-31 01:30:00 EDT" > window(tser[,1],A,B) Error in window.default(x, ...) : 'start' cannot be after 'end' In addition: Warning message: In window.default(x, ...) : 'end' value not changed 

Any tips?

+4
source share
1 answer

When you created the time series object, the ts function expected that the first argument would be data, and that there was no time. (In any case, you probably want to use the objects of the zoo. They make more sense.) See what happens to this:

 > window(tser[,1],start=5,end=8) Time Series: Start = 5 End = 8 Frequency = 1 [1] 1275264900 1275265200 1275265500 1275265800 

Your dates (in numerical representation) have become data!

To use the zoo is pretty straight forward. I'm not sure what you are starting. I had data in a data frame. If you actually have a matrix (which I doubt, since it does not look like output from a matrix object), you can use the [[row, col] "access style."

 require(zoo) zooser <- zoo(x=tser$Close, order.by=as.POSIXct(tser$pTime, origin="1970-01-01")) window(zooser, start=A, end=B) #2010-05-31 01:15:00 2010-05-31 01:20:00 2010-05-31 01:25:00 # 1.2265 1.2265 1.2270 #2010-05-31 01:30:00 # 1.2269 
+5
source

Source: https://habr.com/ru/post/1386874/


All Articles