Skip to Content
R 錦囊妙計
book

R 錦囊妙計

by Paul Teetor
January 2014
Beginner to intermediate
488 pages
8h 1m
Chinese
GoTop Information, Inc.
Content preview from R 錦囊妙計
408
|
第十四章
14.4 選取子時間序列
問題點
您想從時間序列中選擇一個或多個觀測值或時間元素。
解決方案
您可以依據位置為
zoo
xts
物件編制索引。如下所示,使用一個或兩個標記
subscripts)取決於該物件包含一個或多個時間序列:
ts[i]
從單一時間序列資料中,選擇第
i
個觀測值。
ts[j,i]
從多個時間序列資料中,選擇第
j
個時間序列的第
i
個觀測值。
您可以使用日期物件作為時間序列的索引來選擇資料。如下所示,假設時間序列的索引
Date
物件:
> ts[as.Date("yyyy-mm-dd")]
而且,您也可以使用日期的序列作為索引:
> dates <- seq(startdate, enddate, increment)
> ts[dates]
此外,
window
函數可協助選擇開始與結束日期的範圍:
> window(ts, start=startdate, end=enddate)
討論說明
以前述 IBM 股票價格小樣本為例:
> ibm.daily
2010-01-04 2010-01-05 2010-01-06 2010-01-07 2010-01-08
132.45 130.85 130.00 129.55 130.85
如同選擇向量中的元素(錦囊 2.9);我們可以根據位置選擇一個時間序列觀測值:
> ibm.daily[2]
2010-01-05
130.85
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python数据科学

Python数据科学

肯尼迪·贝曼, 诺亚·吉夫特
Java数据科学实战

Java数据科学实战

Michael R. Brzustowicz, PhD

Publisher Resources

ISBN: 9789862769829