How do I subset an xts object to only include weekdays (Mon-Fri, with Saturday and Sunday excluded)?
Here's what I'd do:
library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
x <- sample.xts['2007']
x[!weekdays(index(x)) %in% c("Saturday", "Sunday")]
EDIT:
Joshua Ulrich in comments points out a better solution using .indexwday()
, one of a family of built-in accessor functions for extracting pieces of the index of xts
class objects. Also, like Dirk Eddelbuettel's solution, the following should be locale-independent:
x[.indexwday(x) %in% 1:5]