Split xts object by index (date)

I have an xts object that has intraday data:

head(stocks[,1])
                    SMH.close
2009-01-02 09:31:00     17.66
2009-01-02 09:32:00     17.66
2009-01-02 09:33:00     17.64
2009-01-02 09:34:00     17.60
2009-01-02 09:35:00     17.58
2009-01-02 09:36:00     17.63

I want to perform various analytics on intraday data, but operations should not cross the boundaries of the day. So I want to split the data by date (ignoring the time). So I extracted the index and saved the unique date values:

y <- index(stocks)
x <- strptime(y, format="%Y-%m-%d")
uniquedates <- unique(x)

Now I would like to do something similar to the example in the split section

> g <- airquality$Month
> l <- split(airquality, g)

Here, security data is broken down into a list object by the value of the Month column. I'm not sure how to do something like this, since a date is an index in my case, not a data column. I tried but got an error.

> split(stocks, uniquedates)
Error in args[[i]] : subscript out of bounds

There may be a cleaner way to achieve what I want to do. I would really appreciate your help.

+3
source share
2

unique(). split.xts f="days".

data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
split.xts(sample.xts, f="days")
[[1]]
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778

[[2]]
              Open     High     Low    Close
2007-01-03 50.2305 50.42188 50.2305 50.39767

[[3]]
               Open     High      Low    Close
2007-01-04 50.42096 50.42096 50.26414 50.33236

[[4]]
snipped
+2

, apply.daily ( period.apply )?

0

All Articles