R 子集 XTS 工作日 [英] R Subset XTS weekdays

查看:44
本文介绍了R 子集 XTS 工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 xts 对象子集化为仅包含工作日(周一至周五,周六和周日除外)?

How do I subset an xts object to only include weekdays (Mon-Fri, with Saturday and Sunday excluded)?

推荐答案

这是我要做的:

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")]

<小时>

编辑:Joshua Ulrich 在评论中指出了使用 .indexwday() 的更好解决方案,这是用于提取 xts 类对象的索引片段的一系列内置访问器函数之一.此外,像 Dirk Eddelbuettel 的解决方案一样,以下内容应该与语言环境无关:


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]

这篇关于R 子集 XTS 工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆