-
-
Save anonymous/59e9ef028e98fb4def55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 岡田昌史(著者代表)『Rパッケージガイドブック』東京図書 | |
# http://www.tokyo-tosho.co.jp/books/ISBN978-4-489-02097-1.html | |
# | |
# Google http://www.google.com/search?q=ISBN978-4-489-02097-1 | |
# Amazon http://www.amazon.co.jp/dp/448902097X | |
# | |
# オリジナルのままであればソースファイルの再配布は自由です。 | |
# xts.R | |
# 20110425版 | |
library(xts) | |
data(sample_matrix) | |
sample.xts <- as.xts(sample_matrix) | |
class(sample.xts) | |
str(sample.xts) | |
# 通常の抽出(1 ~ 5 番目までのデータ) | |
sample.xts[1:5] | |
#2007 年1 月05 日から2007 年1 月10 日まで | |
sample.xts['2007-01-05::2007-01-10'] | |
# 最初から2007 年1 月10 日まで | |
sample.xts['::2007-01-10'] | |
#2007 年1 月3 日のみ | |
sample.xts['2007-01-03'] | |
index.monthlast <- endpoints(sample.xts) | |
sample.xts[index.monthlast] | |
period.max(sample.xts[,1],endpoints(sample.xts)) | |
period.apply(sample.xts[,1],endpoints(sample.xts), | |
function(x_){max(x_)}) | |
apply.monthly(sample.xts[,1],mean) | |
apply.weekly(sample.xts[,1],mean) | |
to.monthly(sample.xts) | |
to.weekly(sample.xts) | |
to.period(sample.xts,period="weeks") # 上と同じ | |
index(sample.xts) | |
indexClass(sample.xts) | |
coredata(sample.xts) | |
x <- xts(c(11, NA, 13, 14, 15, NA),as.Date("2010-12-01")+ 0:5) | |
na.locf(x)# 直近値で補間 | |
na.locf(x, fromLast=TRUE) | |
# 日付を逆向きにした直近値で補間(ex: 明日の値で今日の値を補間) | |
na.approx(x, na.rm = FALSE) | |
na.spline(x, na.rm = FALSE) | |
sample.as.xts <- as.xts(sample_matrix) | |
sample.try.xts <- try.xts(sample_matrix) | |
str(sample.as.xts) | |
str(sample.try.xts) | |
class(reclass(sample.as.xts)) | |
class(reclass(sample.try.xts)) | |
change.ratio <- function(x, on="months") | |
{ | |
x <- try.xts(x, error = FALSE) | |
x.index <- endpoints(x, on=on) | |
reclass(x[x.index]/lag(x[x.index])-1, x[x.index]) | |
} | |
(change.ratio.xts <- change.ratio(sample.as.xts[1:100])) | |
(change.ratio.matrix <- change.ratio(sample_matrix[1:100,])) | |
class(change.ratio.xts) | |
class(change.ratio.matrix) | |
x <- zoo(rnorm(1000),Sys.Date()+1:1000) | |
(change.ratio.x <- change.ratio(x,on="years")) | |
class(change.ratio.x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment