Subset Xts Object by Time of Day

Subset xts object by time of day

If your xts object is called x then something like y <- x["T09:30/T11:00"] works for me to get a slice of the morning session, for example.

Subset xts time-series object in R

This is a difficult problem to solve generally, so you need to roll your own solution. The easiest is to use window to subset by overlapping 2-hour intervals.

# initialize a result object
ob2 <- ob * NA_real_
# loop over all rows and calculate 2-hour mean
for(i in 2:nrow(ob)) {
ix <- index(ob)[i]
ob2[i] <- mean(window(ob, start=ix-3600*2, end=ix))
}
# set incomplete 2-hour intervals to NA
is.na(ob2) <- which(index(ob2) < start(ob2)+3600*2)

How to Subset xts file with date object?


dat.subset <-dat[paste0(date.1, "/")]

Subset xts object using variables for start and end periods

If var1 and var2 are variables, then the filter string can be specified using paste as:

 usagexts[paste(var1, var2, sep="/")] 

How to subset xts by Date sequence that considers business days

By business day, I guess you mean a trading day for AAPL, in which case your business days are really the time indices of the AAPL security.

First principles kind of approach using the fact dates can increment by 1 :

indx <- seq(as.Date('2003-03-31'), length.out=200, by='4 weeks')
indx <- indx[indx < as.Date(end(AAPL))]
while(!all(indx %in% as.Date(index(AAPL)))) {
# You ask for the next available business day:
indx[!indx %in% as.Date(index(AAPL))] <- indx[!indx %in% as.Date(index(AAPL))] + 1
# Careful that the last indx value does not go past as.Date(end(AAPL))
if (indx[length(indx)] > as.Date(end(AAPL))) {
indx[length(indx)] <- as.Date(end(AAPL))
}
}

SELECT <- AAPL[indx]
tail(SELECT)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
# 2016-03-14 101.91 102.91 101.78 102.52 25076100 101.35055
# 2016-04-11 108.97 110.61 108.83 109.02 29407500 107.77640
# 2016-05-09 93.00 93.77 92.59 92.79 32936400 92.29005
# 2016-06-06 97.99 101.89 97.55 98.63 23292500 98.09858
# 2016-07-05 95.39 95.40 94.46 94.99 27705200 94.47819
# 2016-08-01 104.41 106.15 104.41 106.05 38167900 105.47860

You might also find solutions via timeDate package useful more generally for business date type subsetting. e.g. http://stackoverflow.com/questions/13673895/r-time-series-data-daily-only-working-days

subset xts object if one column meets some criteria within same day

Here is a solution:

##split idx object with respect to days
aa <- split.xts(x, f="days")

## get indices of days for which x2 == 0 less than 300 times
idx <- which(lapply(aa, function(xx){length(which(xx[,"x2"]==0))}) <= 300)

idx
[1] 2 3 4

##make one xts object containing only the desired days
new.x <- do.call(rbind, aa[idx])

dim(x)
[1] 5760 2

dim(new.x)
[1] 4320 2

xts: subset same intraday time range across different dates

Since you are asking for an xts solution, it is pretty straightforward. Just subset by using the brackets.

t<-read.table("test.csv", sep=";", header = T)
library(xts)
t2<-xts(t[,2:5], order.by = as.POSIXct(t$Timestamp, tz="UTC","%Y-%m-%d %H:%M:%S"))
t2["T09:00/T17:00"]

Alternative version using the nice builtin index functions (this can be used for more complicated operations):

 t2[.indexhour(t2) %in% seq(9,17)]

Return data subset time frames within another timeframes?

You can use the .index* family of functions to get certain months or certain days of the month. See ?index for the full list of functions. For example:

library(quantmod)
getSymbols("SPY")
SPY[.indexmon(SPY)==0] # January for all years (note zero-based indexing!)
SPY[.indexmday(SPY)==1] # The first of every month
SPY[.indexwday(SPY)==1] # All Mondays


Related Topics



Leave a reply



Submit