这是我的数据框:
structure(list(dates = structure(c(16162, 16161, 16160, 16157,
16156, 16155, 16154, 16153, 16150, 16149, 16148, 16147, 16146,
16143, 16142, 16141, 16140, 16139, 16136, 16135, 16134, 16129,
16128, 16127, 16126, 16125, 16122, 16121, 16120, 16119, 16118,
16115, 16114, 16113, 16112, 16111, 16108, 16107, 16106, 16105,
16104, 16101, 16100, 16099, 16098, 16097, 16094, 16093, 16092,
16091), class = "Date"), VALE5 = c(28.29, 28.26, 28.35, 27.81,
27.85, 27.5, 27.61, 27.16, 27.2, 26.64, 26.57, 26.55, 26, 26.1,
25.9, 26.46, 26.1, 26.37, 27.09, 28.11, 28.11, 29.09, 29.31,
29.02, 29, 29.76, 30.61, 30.59, 30.9, 30.6, 30.74, 30.96, 30.76,
30.79, 30.77, 30.44, 30.66, 30.8, 29.94, 29.58, 29.1, 30, 29.76,
29.96, 28.88, 28.54, 28.63, 28.15, 28.91, 28.48)), row.names = c(NA,
50L), class = "data.frame")
我想要这样设置窗口功能:
window(sample,start=c(2014,03,26),end=c(2014,04,02))
我的sample
数据帧出了什么问题? dates列具有类Date
。使用window
函数还不够吗?
我该如何解决?
非常感谢。
答案 0 :(得分:4)
1)window.zoo window
用于诸如ts
和zoo
之类的时间序列对象。 ts
不适用于日常数据,但是您可以将sample
转换为动物园对象,然后像这样运行它:
library(zoo)
z <- read.zoo(sample)
window(z, start = "2014-03-26", end = "2014-04-02")
给予:
BBAS3 BBDC3 VALE5
2014-03-26 21.11 31.53 27.50
2014-03-27 22.51 33.38 27.85
2014-03-28 22.22 33.20 27.81
2014-03-31 22.80 33.59 28.35
2014-04-01 22.78 33.75 28.26
2014-04-02 22.85 33.82 28.29
2)子集:此基本方法也可以使用,但不使用window
:
subset(sample, dates >= "2014-03-26" & dates <= "2014-04-02")
3)xts 对于xts,可以使用此表示法。 z
来自以上(1):
library(xts)
x <- as.xts(z)
x["2014-03-26/2014-04-02"]