(now_ct <- Sys.time() )
## [1] "2015-11-01 23:25:01 CST"
POSIXct 透過數值儲存 從1970/01/01 開始的秒數
unclass(now_ct)
## [1] 1446391502
(now_lt <- as.POSIXlt(now_ct))
## [1] "2015-11-01 23:25:01 CST"
POSIXlt 用list 型態儲存日期資訊
unclass(now_lt)
## $sec
## [1] 1.816215
##
## $min
## [1] 25
##
## $hour
## [1] 23
##
## $mday
## [1] 1
##
## $mon
## [1] 10
##
## $year
## [1] 115
##
## $wday
## [1] 0
##
## $yday
## [1] 304
##
## $isdst
## [1] 0
##
## $zone
## [1] "CST"
##
## $gmtoff
## [1] 28800
##
## attr(,"tzone")
## [1] "" "CST" "CDT"
透過list的方式存取時間資訊
now_lt$sec
## [1] 1.816215
now_lt[["min"]]
## [1] 25
now_lt$s
## [1] 1.816215
ls(now_lt)
## [1] "gmtoff" "hour" "isdst" "mday" "min" "mon" "sec"
## [8] "wday" "yday" "year" "zone"
year 從 1900 開始計算 需修正 1900 才能取得西元年月
now_lt$year + 1900
## [1] 2015
now_date <- as.Date(now_ct)
用數值儲存從1970/01/01開始的天數
unclass(now_date)
## [1] 16740
unclass(now_ct)/24/60/60
## [1] 16740.64
%H: Hours as decimal number (00–23)
%M: Minute as decimal number (00–59)
%m: Month as decimal number (01–12)
%d: Day of the month as decimal number (01–31)
%S: Second as decimal number (00–61)
%Y: Year with century
%T: Equivalent to %H:%M:%S
%F: Equivalent to %Y-%m-%d
moon_landing_str <- c(
"20:17:40 20/07/1969",
"06:54:35 19/11/1969",
"09:18:11 05/02/1971"
)
( moon_landing_lt <- strptime(moon_landing_str,
format="%H:%M:%S %d/%m/%Y",
tz = "UTC") )
## [1] "1969-07-20 20:17:40 UTC" "1969-11-19 06:54:35 UTC"
## [3] "1971-02-05 09:18:11 UTC"
( moon_landing_lt <- strptime(moon_landing_str,
format="%T %d/%m/%Y",
tz = "UTC") )
## [1] "1969-07-20 20:17:40 UTC" "1969-11-19 06:54:35 UTC"
## [3] "1971-02-05 09:18:11 UTC"
透過 ISOdate依傳入 參數產生日期型態
ISOdatetime(year, month, day, hour, min, sec, tz = “”)
ISOdate(year, month, day, hour = 12, min = 0, sec = 0, tz = “GMT”)
(d1 <- ISOdate(2013, 2, 20))
## [1] "2013-02-20 12:00:00 GMT"
str(d1)
## POSIXct[1:1], format: "2013-02-20 12:00:00"
years <- 2010:2014
months <- c(1,1,1,1,1)
days <- c(15,21,20,18,17)
(isodate <- ISOdate(years, months, days))
## [1] "2010-01-15 12:00:00 GMT" "2011-01-21 12:00:00 GMT"
## [3] "2012-01-20 12:00:00 GMT" "2013-01-18 12:00:00 GMT"
## [5] "2014-01-17 12:00:00 GMT"
向量處理長度不同會自動 repeat 填補,不過,長度不一致沒有警告消息
ISOdate(2010:2014, 1, 20:21)
## [1] "2010-01-20 12:00:00 GMT" "2011-01-21 12:00:00 GMT"
## [3] "2012-01-20 12:00:00 GMT" "2013-01-21 12:00:00 GMT"
## [5] "2014-01-20 12:00:00 GMT"
依設定格式,將日期成文字型別
strftime(moon_landing_lt, "%p")
## [1] "下午" "上午" "上午"
str(strftime(moon_landing_lt, "%p"))
## chr [1:3] "下午" "上午" "上午"
strftime(isodate, "It's %I:%M%p on %A %d %B, %Y")
## [1] "It's 08:00下午 on 周五 15 1月, 2010"
## [2] "It's 08:00下午 on 周五 21 1月, 2011"
## [3] "It's 08:00下午 on 周五 20 1月, 2012"
## [4] "It's 08:00下午 on 周五 18 1月, 2013"
## [5] "It's 08:00下午 on 周五 17 1月, 2014"
strftime(isodate, "It's %A")
## [1] "It's 周五" "It's 周五" "It's 周五" "It's 周五" "It's 周五"
轉換成不同時區
now <- Sys.time()
strftime(now, tz="Asia/Tokyo")
## [1] "2015-11-02 00:25:02"
strftime(now, tz="Asia/Taipei")
## [1] "2015-11-01 23:25:02"
透過 as.Date 解析 Date
mydates <- as.Date(c("2007-06-22","2004-02-13"))
strDates <- c("01/05/1965","08/16/1975")
dates <- as.Date(strDates, "%m/%d/%Y")
strDates1 <- c("01/05/65","08/16/75","08/16/15","08/16/20","08/16/30","08/16/50",
"08/16/66","08/16/67","08/16/68","08/16/69","08/16/70")
dates1 <- as.Date(strDates1, "%m/%d/%y")
dates1
## [1] "2065-01-05" "1975-08-16" "2015-08-16" "2020-08-16" "2030-08-16"
## [6] "2050-08-16" "2066-08-16" "2067-08-16" "2068-08-16" "1969-08-16"
## [11] "1970-08-16"
日期相減 透過 difftime 儲存日期 差異
strdate <- as.Date("2004-02-13")
enddate <- as.Date("2011-01-22")
days <- enddate - strdate
檢視 difftime的資料形態
str(days)
## Class 'difftime' atomic [1:1] 2535
## ..- attr(*, "units")= chr "days"
class(days)
## [1] "difftime"
as.numeric(days, unit="secs")
## [1] 219024000
as.numeric(days, unit="days")
## [1] 2535
today <- Sys.Date()
format(today, format="%B %d %Y")
## [1] "11月 01 2015"
format(today, format="%A")
## [1] "周日"
birthday <- as.Date("1974-06-29")
difftime(today, birthday, unit="weeks") / 52
## Time difference of 41.48352 weeks
透過 seq產生一連串日期
the_str_time <- as.Date("1970-01-01")
the_end_time <- as.Date("2012-12-21")
逐年產生日期數列
dates.1 <- seq(the_str_time, the_end_time, by = "year")
head(dates.1)
## [1] "1970-01-01" "1971-01-01" "1972-01-01" "1973-01-01" "1974-01-01"
## [6] "1975-01-01"
tail(dates.1)
## [1] "2007-01-01" "2008-01-01" "2009-01-01" "2010-01-01" "2011-01-01"
## [6] "2012-01-01"
逐天產生日期數列
dates.2 <- seq(the_str_time, the_end_time, by = 1)
head(dates.2)
## [1] "1970-01-01" "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05"
## [6] "1970-01-06"
tail(dates.2)
## [1] "2012-12-16" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20"
## [6] "2012-12-21"
逐月產生日期數列
the_str_time_w <- as.Date("1972-01-31")
the_end_time_w <- as.Date("2012-12-21")
dates.3 <- seq(the_str_time_w, the_end_time_w, by = "month")
head(dates.3)
## [1] "1972-01-31" "1972-03-02" "1972-03-31" "1972-05-01" "1972-05-31"
## [6] "1972-07-01"
tail(dates.3)
## [1] "2012-07-01" "2012-07-31" "2012-08-31" "2012-10-01" "2012-10-31"
## [6] "2012-12-01"
require(lubridate)
## Loading required package: lubridate
(lubri.date <- ymd("1972/02/29"))
## [1] "1972-02-29 UTC"
class(lubri.date)
## [1] "POSIXct" "POSIXt"
(lubri.time <- ymd_hms("2015/10/20 21:38:00", tz="Asia/Taipei"))
## [1] "2015-10-20 21:38:00 CST"
class(lubri.time)
## [1] "POSIXct" "POSIXt"
透過wday取得星期幾 (1-7, Sunday is 1)
wday(lubri.time)
## [1] 3
wday(lubri.time, label = TRUE, abbr = FALSE)
## [1] Tuesday
## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday
lubridate可以自動判斷可能的格式解析日期
ymd("2015 10 10")
## [1] "2015-10-10 UTC"
ymd("2015/10.10")
## [1] "2015-10-10 UTC"
ymd("2015-10/10")
## [1] "2015-10-10 UTC"
ymd("2015 3 23")
## [1] "2015-03-23 UTC"
可以透過函式設值可以改變時間,e.g.計算分鐘平均數,修正時間的分鐘為平均數
dates <- ymd_hms(c("2015-01-01 01:01:00", "2015-01-30 01:30:00"))
minute(dates) <- mean(minute(dates))
dates
## [1] "2015-01-01 01:15:00 UTC" "2015-01-30 01:15:00 UTC"
改變日為31號, 如果當月無這個日數,會往後取得次月天數
date <- now()
day(date) <- 31
date
## [1] "2015-12-01 23:25:02 CST"
透過month設值改變日期
month(date) <- month(date) -1
date
## [1] "2015-11-01 23:25:02 CST"
month(date) <- 12
date
## [1] "2015-12-01 23:25:02 CST"
day(date) <- 31
透過 period hours, months,years, days 改變日期
date
## [1] "2015-12-31 23:25:02 CST"
date + hours(3)
## [1] "2016-01-01 02:25:02 CST"
date + months(1)
## [1] "2016-01-31 23:25:02 CST"
date + years(1)
## [1] "2016-12-31 23:25:02 CST"
date + days(1)
## [1] "2016-01-01 23:25:02 CST"
leap.date <- ymd("2012-01-01")
leap.date + years(1)
## [1] "2013-01-01 UTC"
year(leap.date) <- 2013
tz <- Sys.timezone()
(start_2011 <- ymd_hms("2011-01-01 12:00:00") )
## [1] "2011-01-01 12:00:00 UTC"
(start_2010 <- ymd_hms("2010-01-01 12:00:00") )
## [1] "2010-01-01 12:00:00 UTC"
span <- start_2011 - start_2010
class(span)
## [1] "difftime"
span
## Time difference of 365 days
span1 <- new_interval(start_2010, start_2011)
class(span1)
## [1] "Interval"
## attr(,"package")
## [1] "lubridate"
int_start(span1)
## [1] "2010-01-01 12:00:00 UTC"
span2 <- start_2010 %--% start_2011
span2
## [1] 2010-01-01 12:00:00 UTC--2011-01-01 12:00:00 UTC
class(span2)
## [1] "Interval"
## attr(,"package")
## [1] "lubridate"
int_start(span2) <- ymd_hms("2001-11-01 12:00:00")
int_end(span2)
## [1] "2011-01-01 12:00:00 UTC"
span2
## [1] 2001-11-01 12:00:00 UTC--2011-01-01 12:00:00 UTC
# 不能透過 interval 調整日期 e.g. start_2011 + span2
(span11 <- new_interval(start_2010, start_2011) )
## [1] 2010-01-01 12:00:00 UTC--2011-01-01 12:00:00 UTC
(span12 <- new_interval(start_2010, start_2011, tzone= "Asia/Tokyo") )
## [1] 2010-01-01 21:00:00 JST--2011-01-01 21:00:00 JST
(span13 <- new_interval(start_2010, start_2011, tzone= "Asia/Taipei") )
## [1] 2010-01-01 20:00:00 CST--2011-01-01 20:00:00 CST
df <- data.frame(A= c("22","32","50", "42"))
df$A
## [1] 22 32 50 42
## Levels: 22 32 42 50
df
## A
## 1 22
## 2 32
## 3 50
## 4 42
as.numeric(df$A)
## [1] 1 2 4 3
subset(df, A > 30)
## Warning in Ops.factor(A, 30): '>' not meaningful for factors
## [1] A
## <0 rows> (or 0-length row.names)
#需要宣導 字串不轉為 factor 才能透過字串篩選
df1 <- data.frame(A= c("22","32","50", "42"), stringsAsFactors = FALSE)
subset(df1, A > "30")
## A
## 2 32
## 3 50
## 4 42
new_duration(60)
## [1] "60s"
new_duration(day = 1, week= 1)
## [1] "691200s (~8 days)"
1:3 * dhours(2)
## [1] "7200s (~2 hours)" "14400s (~4 hours)" "21600s (~6 hours)"
class(dyears(1))
## [1] "Duration"
## attr(,"package")
## [1] "lubridate"
# 2012閏年 透過 duration + 兩年會用每年固定天數 調整為 2012/12/31
start_2011 + dyears(2)
## [1] "2012-12-31 12:00:00 UTC"
dyears(2)
## [1] "63072000s (~2 years)"
class(years(1))
## [1] "Period"
## attr(,"package")
## [1] "lubridate"
start_2011 + years(2)
## [1] "2013-01-01 12:00:00 UTC"
months(1)
## [1] "1m 0d 0H 0M 0S"
(test.date <- ymd_hms("2001-10-31 12:00:00"))
## [1] "2001-10-31 12:00:00 UTC"
# 次月無改日期,回傳 NA
test.date + months(1)
## [1] NA
判斷日期是否落在日期區間內
span2
## [1] 2001-11-01 12:00:00 UTC--2011-01-01 12:00:00 UTC
start_2010 %within% span2
## [1] TRUE
start_2011 %within% span2
## [1] TRUE
test.date %within% span2
## [1] FALSE
將日期修整到指定間隔上
april20 <- ymd_hms("2010-04-20 11:33:29")
round_date(april20, "day")
## [1] "2010-04-20 UTC"
round_date(april20, "month")
## [1] "2010-05-01 UTC"
round_date(april20, "year")
## [1] "2010-01-01 UTC"
april15 <- ymd_hms("2015-04-15 12:33:29")
round_date(april15, "month")
## [1] "2015-04-01 UTC"
round_date(april15, "day")
## [1] "2015-04-16 UTC"
oct23 <- ymd_hms("2015-10-23 12:33:33")
round_date(oct23, "month")
## [1] "2015-11-01 UTC"
round_date(oct23, "day")
## [1] "2015-10-24 UTC"
round_date(oct23, "year")
## [1] "2016-01-01 UTC"
round_date(oct23, "week")
## [1] "2015-10-25 UTC"
round_date(oct23, "hour")
## [1] "2015-10-23 13:00:00 UTC"
round_date(oct23, "minute")
## [1] "2015-10-23 12:34:00 UTC"
round_date(oct23, "second")
## [1] "2015-10-23 12:33:33 UTC"
找到每個月最後一天
ceiling_date(oct23, "month") - days(1)
## [1] "2015-10-31 UTC"
start_date <- start_2010 找到感恩節,11月的第四個星期四
findThanksGiving <- function(start_date) {
month(start_date) <- 11
start_date <- floor_date(start_date, "month")
#print(start_date)
wday(start_date, label = TRUE, abbr= FALSE)
shift.to.thursday <- 5 - wday(start_date)
#print(shift.to.thursday)
if(shift.to.thursday < 0) {
shift.to.thursday <- shift.to.thursday + 7
}
first.thursday <- start_date + days(shift.to.thursday )
#print(first.thursday)
thanks.giving <- first.thursday + weeks(3)
return (thanks.giving)
}
findThanksGiving(start_2010)
## [1] "2010-11-25 UTC"
findThanksGiving(ymd("2014-1-1"))
## [1] "2014-11-27 UTC"
xts套件將資料轉換成時間序列
require(xts)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
data(sample_matrix)
head(sample_matrix)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
class(sample_matrix)
## [1] "matrix"
dim(sample_matrix)
## [1] 180 4
str(sample_matrix)
## num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:180] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
## ..$ : chr [1:4] "Open" "High" "Low" "Close"
apply(sample_matrix, 2, class)
## Open High Low Close
## "numeric" "numeric" "numeric" "numeric"
head(row.names(sample_matrix))
## [1] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-06"
## [6] "2007-01-07"
colnames(sample_matrix)
## [1] "Open" "High" "Low" "Close"
matrix_xts <- as.xts(sample_matrix, dateFormat = 'Date')
str(matrix_xts)
## An 'xts' object on 2007-01-02/2007-06-30 containing:
## Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:4] "Open" "High" "Low" "Close"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
dim(matrix_xts)
## [1] 180 4
class(matrix_xts)
## [1] "xts" "zoo"
row.names(matrix_xts)
## NULL
colnames(matrix_xts)
## [1] "Open" "High" "Low" "Close"
head(matrix_xts)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
df_xts <- as.xts(as.data.frame(sample_matrix),
#dateFormat = 'Date',
important = 'very important info!' ,
rowname = row.names(sample_matrix))
str(df_xts)
## An 'xts' object on 2007-01-02/2007-06-30 containing:
## Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:4] "Open" "High" "Low" "Close"
## Indexed by objects of class: [POSIXct,POSIXt] TZ:
## xts Attributes:
## List of 2
## $ important: chr "very important info!"
## $ rowname : chr [1:180] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
head(df_xts)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
new.xts <- xts(1:10, Sys.Date() + 1:10)
colnames(new.xts) <- c("names")
str(new.xts)
## An 'xts' object on 2015-11-02/2015-11-11 containing:
## Data: int [1:10, 1] 1 2 3 4 5 6 7 8 9 10
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr "names"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
coredata(new.xts)
## names
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
## [9,] 9
## [10,] 10
time.xts <- xts(1:10, now() + 1:10)
colnames(time.xts) <- c("names")
str(time.xts)
## An 'xts' object on 2015-11-01 23:25:04/2015-11-01 23:25:13 containing:
## Data: int [1:10, 1] 1 2 3 4 5 6 7 8 9 10
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr "names"
## Indexed by objects of class: [POSIXct,POSIXt] TZ:
## xts Attributes:
## NULL
new.xts
## names
## 2015-11-02 1
## 2015-11-03 2
## 2015-11-04 3
## 2015-11-05 4
## 2015-11-06 5
## 2015-11-07 6
## 2015-11-08 7
## 2015-11-09 8
## 2015-11-10 9
## 2015-11-11 10
head(time.xts)
## names
## 2015-11-01 23:25:04 1
## 2015-11-01 23:25:05 2
## 2015-11-01 23:25:06 3
## 2015-11-01 23:25:07 4
## 2015-11-01 23:25:08 5
## 2015-11-01 23:25:09 6
透過 coredata 與 index取得時間序列的資料與日期指標
prices <- c(132.45, 130.85, 130.00, 129.55, 130.85)
dates <- seq(as.Date("2010-01-04"), as.Date("2010-01-08"), by = "day")
ibm.daily <- zoo(prices, dates)
coredata(ibm.daily)
## [1] 132.45 130.85 130.00 129.55 130.85
str(coredata(ibm.daily))
## num [1:5] 132 131 130 130 131
index(ibm.daily)
## [1] "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
str(index(ibm.daily))
## Date[1:5], format: "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" ...
attributes(ibm.daily)
## $index
## [1] "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
##
## $class
## [1] "zoo"
# xts 用矩陣儲存 coredata
ibm.daily.1 <- xts(prices, dates)
coredata(ibm.daily.1)
## [,1]
## [1,] 132.45
## [2,] 130.85
## [3,] 130.00
## [4,] 129.55
## [5,] 130.85
str(coredata(ibm.daily.1))
## num [1:5, 1] 132 131 130 130 131
class(coredata(ibm.daily.1))
## [1] "matrix"
index(ibm.daily.1)
## [1] "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" "2010-01-08"
str(index(ibm.daily.1))
## Date[1:5], format: "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07" ...
attributes(ibm.daily.1)
## $dim
## [1] 5 1
##
## $index
## [1] 1262563200 1262649600 1262736000 1262822400 1262908800
## attr(,"tzone")
## [1] "UTC"
## attr(,"tclass")
## [1] "Date"
##
## $class
## [1] "xts" "zoo"
##
## $.indexCLASS
## [1] "Date"
##
## $tclass
## [1] "Date"
##
## $.indexTZ
## [1] "UTC"
##
## $tzone
## [1] "UTC"
透過[] 篩選時間序列指定區間的資料,格式 yyyy[-mm[-dd]] 區間頭尾用 / or :: 分隔
matrix_xts.200703 <- matrix_xts['2007-03']
dim(matrix_xts.200703)
## [1] 31 4
matrix_xts.200735 <- matrix_xts['2007-03/2007-05']
dim(matrix_xts.200735)
## [1] 92 4
matrix_xts['/2007-05']
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
## 2007-01-11 49.88529 50.23910 49.88529 50.23910
## 2007-01-12 50.21258 50.35980 50.17176 50.28519
## 2007-01-13 50.32385 50.48000 50.32385 50.41286
## 2007-01-14 50.46359 50.62395 50.46359 50.60145
## 2007-01-15 50.61724 50.68583 50.47359 50.48912
## 2007-01-16 50.62024 50.73731 50.56627 50.67835
## 2007-01-17 50.74150 50.77336 50.44932 50.48644
## 2007-01-18 50.48051 50.60712 50.40269 50.57632
## 2007-01-19 50.41381 50.55627 50.41278 50.41278
## 2007-01-20 50.35323 50.35323 50.02142 50.02142
## 2007-01-21 50.16188 50.42090 50.16044 50.42090
## 2007-01-22 50.36008 50.43875 50.21129 50.21129
## 2007-01-23 50.03966 50.16961 50.03670 50.16961
## 2007-01-24 50.10953 50.26942 50.06387 50.23145
## 2007-01-25 50.20738 50.28268 50.12913 50.24334
## 2007-01-26 50.16008 50.16008 49.94052 50.07024
## 2007-01-27 50.06041 50.09777 49.97267 50.01091
## 2007-01-28 49.96586 50.00217 49.87468 49.88096
## 2007-01-29 49.85624 49.93038 49.76308 49.91875
## 2007-01-30 49.85477 50.02180 49.77242 50.02180
## 2007-01-31 50.07049 50.22578 50.07049 50.22578
## 2007-02-01 50.22448 50.41376 50.19101 50.35784
## 2007-02-02 50.44503 50.53490 50.36064 50.36928
## 2007-02-03 50.37219 50.46912 50.29880 50.43109
## 2007-02-04 50.48183 50.55509 50.40203 50.55509
## 2007-02-05 50.52389 50.69783 50.45977 50.69783
## 2007-02-06 50.71661 50.71661 50.49865 50.49865
## 2007-02-07 50.49322 50.69693 50.49322 50.60611
## 2007-02-08 50.58531 50.84734 50.58531 50.81383
## 2007-02-09 50.83331 50.89683 50.67686 50.67686
## 2007-02-10 50.68923 50.72696 50.60707 50.69562
## 2007-02-11 50.67849 50.91776 50.67849 50.91160
## 2007-02-12 50.88990 50.96653 50.83604 50.96653
## 2007-02-13 50.90056 51.00299 50.87935 50.90106
## 2007-02-14 50.95283 51.04699 50.80317 51.04699
## 2007-02-15 51.06330 51.11401 50.94681 51.05185
## 2007-02-16 51.12879 51.12879 51.00613 51.02164
## 2007-02-17 50.97722 51.13653 50.95260 51.13653
## 2007-02-18 51.18414 51.32090 51.13713 51.15151
## 2007-02-19 51.29502 51.32342 51.13524 51.17899
## 2007-02-20 51.13725 51.14940 50.93523 50.93523
## 2007-02-21 50.92940 50.92940 50.69880 50.77325
## 2007-02-22 50.72111 50.86597 50.65718 50.86597
## 2007-02-23 50.84392 50.96946 50.73060 50.76498
## 2007-02-24 50.78360 50.86453 50.76692 50.79534
## 2007-02-25 50.78960 50.93187 50.78960 50.84776
## 2007-02-26 50.88168 50.88168 50.75481 50.75481
## 2007-02-27 50.74333 50.78909 50.61874 50.69206
## 2007-02-28 50.69435 50.77091 50.59881 50.77091
## 2007-03-01 50.81620 50.81620 50.56451 50.57075
## 2007-03-02 50.60980 50.72061 50.50808 50.61559
## 2007-03-03 50.73241 50.73241 50.40929 50.41033
## 2007-03-04 50.39273 50.40881 50.24922 50.32636
## 2007-03-05 50.26501 50.34050 50.26501 50.29567
## 2007-03-06 50.27464 50.32019 50.16380 50.16380
## 2007-03-07 50.14458 50.20278 49.91381 49.91381
## 2007-03-08 49.93149 50.00364 49.84893 49.91839
## 2007-03-09 49.92377 49.92377 49.74242 49.80712
## 2007-03-10 49.79370 49.88984 49.70385 49.88698
## 2007-03-11 49.83062 49.88295 49.76031 49.78806
## 2007-03-12 49.82763 49.90311 49.67049 49.74033
## 2007-03-13 49.69628 49.70863 49.37924 49.37924
## 2007-03-14 49.36270 49.53735 49.30746 49.53735
## 2007-03-15 49.57374 49.62310 49.39876 49.49600
## 2007-03-16 49.44900 49.65285 49.42416 49.59500
## 2007-03-17 49.55666 49.55666 49.33564 49.34714
## 2007-03-18 49.29778 49.67857 49.29778 49.65463
## 2007-03-19 49.62747 49.65407 49.51604 49.54590
## 2007-03-20 49.59529 49.62003 49.42321 49.50690
## 2007-03-21 49.49765 49.53961 49.41610 49.51807
## 2007-03-22 49.42306 49.42306 49.31184 49.39687
## 2007-03-23 49.27281 49.27281 48.93095 48.93095
## 2007-03-24 48.86635 48.86635 48.52684 48.52684
## 2007-03-25 48.50649 48.50649 48.33409 48.33973
## 2007-03-26 48.34210 48.44637 48.28969 48.28969
## 2007-03-27 48.25248 48.41572 48.23648 48.30851
## 2007-03-28 48.33090 48.53595 48.33090 48.53595
## 2007-03-29 48.59236 48.69988 48.57432 48.69988
## 2007-03-30 48.74562 49.00218 48.74562 48.93546
## 2007-03-31 48.95616 49.09728 48.95616 48.97490
## 2007-04-01 48.94407 48.97816 48.80962 48.87032
## 2007-04-02 48.90488 49.08400 48.90488 49.06316
## 2007-04-03 49.06071 49.24525 48.96928 49.24525
## 2007-04-04 49.22579 49.37335 49.19913 49.34736
## 2007-04-05 49.41435 49.41435 49.30641 49.33776
## 2007-04-06 49.33621 49.41900 49.33621 49.41900
## 2007-04-07 49.45170 49.60950 49.45170 49.53819
## 2007-04-08 49.54338 49.58968 49.41806 49.41806
## 2007-04-09 49.44429 49.50234 49.33828 49.50234
## 2007-04-10 49.55704 49.78776 49.55704 49.76984
## 2007-04-11 49.74550 49.81925 49.74550 49.74623
## 2007-04-12 49.75079 49.75470 49.61732 49.72996
## 2007-04-13 49.70708 49.85332 49.69245 49.73339
## 2007-04-14 49.74154 49.77340 49.70159 49.75552
## 2007-04-15 49.74707 49.79341 49.66299 49.70942
## 2007-04-16 49.74915 49.86289 49.71091 49.83886
## 2007-04-17 49.84698 49.95456 49.77754 49.95456
## 2007-04-18 49.93794 50.07208 49.92484 50.07208
## 2007-04-19 50.02441 50.02991 49.83945 49.83945
## 2007-04-20 49.76042 49.92847 49.69808 49.91103
## 2007-04-21 49.98954 50.20123 49.98954 50.20123
## 2007-04-22 50.31203 50.33781 50.24788 50.32556
## 2007-04-23 50.32009 50.32009 49.87574 49.88539
## 2007-04-24 49.87340 49.90184 49.72769 49.72769
## 2007-04-25 49.73385 49.88622 49.73385 49.88472
## 2007-04-26 49.89064 49.89064 49.74899 49.79201
## 2007-04-27 49.80530 49.80530 49.50814 49.50814
## 2007-04-28 49.54688 49.55497 49.29186 49.29186
## 2007-04-29 49.30289 49.30289 49.05676 49.13529
## 2007-04-30 49.13825 49.33974 49.11500 49.33974
## 2007-05-01 49.34572 49.52635 49.34572 49.47138
## 2007-05-02 49.47062 49.47062 49.34261 49.38521
## 2007-05-03 49.46328 49.69097 49.46328 49.58677
## 2007-05-04 49.59963 49.59963 49.41375 49.41375
## 2007-05-05 49.38428 49.40266 49.10310 49.10310
## 2007-05-06 49.16606 49.45999 49.16606 49.45999
## 2007-05-07 49.49188 49.49188 49.13572 49.13572
## 2007-05-08 49.13282 49.25507 49.13282 49.18930
## 2007-05-09 49.17739 49.17739 48.72708 48.72708
## 2007-05-10 48.83479 48.84549 48.38001 48.38001
## 2007-05-11 48.25456 48.25456 47.96904 47.96904
## 2007-05-12 47.96813 48.03286 47.89262 48.01935
## 2007-05-13 48.05550 48.05550 47.66209 47.66209
## 2007-05-14 47.64469 47.72505 47.58212 47.65930
## 2007-05-15 47.60647 47.74053 47.51796 47.72686
## 2007-05-16 47.72065 47.90717 47.70913 47.86683
## 2007-05-17 47.79430 47.79430 47.55140 47.62938
## 2007-05-18 47.65013 47.75117 47.65013 47.68423
## 2007-05-19 47.65552 47.77986 47.60536 47.60536
## 2007-05-20 47.56210 47.93085 47.56210 47.93085
## 2007-05-21 47.96582 48.02903 47.78072 47.78072
## 2007-05-22 47.81830 47.94825 47.81155 47.82946
## 2007-05-23 47.93593 48.08242 47.88763 47.90068
## 2007-05-24 47.89041 48.03077 47.88413 48.01130
## 2007-05-25 47.98234 48.17543 47.94507 48.16058
## 2007-05-26 48.14521 48.14521 47.92649 47.99613
## 2007-05-27 48.01018 48.02166 47.90193 47.90193
## 2007-05-28 47.90142 47.93398 47.64718 47.64718
## 2007-05-29 47.65665 47.89342 47.65446 47.87252
## 2007-05-30 47.78866 47.93267 47.78866 47.83291
## 2007-05-31 47.82845 47.84044 47.73780 47.73780
matrix_xts['2007-03-20/2007-05-10']
## Open High Low Close
## 2007-03-20 49.59529 49.62003 49.42321 49.50690
## 2007-03-21 49.49765 49.53961 49.41610 49.51807
## 2007-03-22 49.42306 49.42306 49.31184 49.39687
## 2007-03-23 49.27281 49.27281 48.93095 48.93095
## 2007-03-24 48.86635 48.86635 48.52684 48.52684
## 2007-03-25 48.50649 48.50649 48.33409 48.33973
## 2007-03-26 48.34210 48.44637 48.28969 48.28969
## 2007-03-27 48.25248 48.41572 48.23648 48.30851
## 2007-03-28 48.33090 48.53595 48.33090 48.53595
## 2007-03-29 48.59236 48.69988 48.57432 48.69988
## 2007-03-30 48.74562 49.00218 48.74562 48.93546
## 2007-03-31 48.95616 49.09728 48.95616 48.97490
## 2007-04-01 48.94407 48.97816 48.80962 48.87032
## 2007-04-02 48.90488 49.08400 48.90488 49.06316
## 2007-04-03 49.06071 49.24525 48.96928 49.24525
## 2007-04-04 49.22579 49.37335 49.19913 49.34736
## 2007-04-05 49.41435 49.41435 49.30641 49.33776
## 2007-04-06 49.33621 49.41900 49.33621 49.41900
## 2007-04-07 49.45170 49.60950 49.45170 49.53819
## 2007-04-08 49.54338 49.58968 49.41806 49.41806
## 2007-04-09 49.44429 49.50234 49.33828 49.50234
## 2007-04-10 49.55704 49.78776 49.55704 49.76984
## 2007-04-11 49.74550 49.81925 49.74550 49.74623
## 2007-04-12 49.75079 49.75470 49.61732 49.72996
## 2007-04-13 49.70708 49.85332 49.69245 49.73339
## 2007-04-14 49.74154 49.77340 49.70159 49.75552
## 2007-04-15 49.74707 49.79341 49.66299 49.70942
## 2007-04-16 49.74915 49.86289 49.71091 49.83886
## 2007-04-17 49.84698 49.95456 49.77754 49.95456
## 2007-04-18 49.93794 50.07208 49.92484 50.07208
## 2007-04-19 50.02441 50.02991 49.83945 49.83945
## 2007-04-20 49.76042 49.92847 49.69808 49.91103
## 2007-04-21 49.98954 50.20123 49.98954 50.20123
## 2007-04-22 50.31203 50.33781 50.24788 50.32556
## 2007-04-23 50.32009 50.32009 49.87574 49.88539
## 2007-04-24 49.87340 49.90184 49.72769 49.72769
## 2007-04-25 49.73385 49.88622 49.73385 49.88472
## 2007-04-26 49.89064 49.89064 49.74899 49.79201
## 2007-04-27 49.80530 49.80530 49.50814 49.50814
## 2007-04-28 49.54688 49.55497 49.29186 49.29186
## 2007-04-29 49.30289 49.30289 49.05676 49.13529
## 2007-04-30 49.13825 49.33974 49.11500 49.33974
## 2007-05-01 49.34572 49.52635 49.34572 49.47138
## 2007-05-02 49.47062 49.47062 49.34261 49.38521
## 2007-05-03 49.46328 49.69097 49.46328 49.58677
## 2007-05-04 49.59963 49.59963 49.41375 49.41375
## 2007-05-05 49.38428 49.40266 49.10310 49.10310
## 2007-05-06 49.16606 49.45999 49.16606 49.45999
## 2007-05-07 49.49188 49.49188 49.13572 49.13572
## 2007-05-08 49.13282 49.25507 49.13282 49.18930
## 2007-05-09 49.17739 49.17739 48.72708 48.72708
## 2007-05-10 48.83479 48.84549 48.38001 48.38001
matrix_xts['2007-01-01/2007-01-10']
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
matrix_xts['20070101::20070110']
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
matrix_xts['2007-1-01/2007-1-10']
## Open High Low Close
matrix_xts['2007-1-01/2007-1-10']
## Open High Low Close
matrix_xts[1]
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
matrix_xts[1,2]
## High
## 2007-01-02 50.11778
matrix_xts[as.Date("2007/1/2")]
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
透過 seq產生固定間隔日日期數列,再用 []篩選資料
select.dates <- seq(as.Date("2007-01-04"), as.Date("2007-01-08"), by = 2)
matrix_xts[select.dates]
## Open High Low Close
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
透過window篩選 區間的資料
window(matrix_xts, start=as.Date("2007/1/2"), end = as.Date("2007/1/10"))
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
透過merge整併兩組資料
x <- xts(4:10, Sys.Date() + 4:10)
y <- xts(1:6, Sys.Date() + 1:6)
merge(x, y, all = FALSE)
## x y
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
merge(x,y )
## x y
## 2015-11-02 NA 1
## 2015-11-03 NA 2
## 2015-11-04 NA 3
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
## 2015-11-08 7 NA
## 2015-11-09 8 NA
## 2015-11-10 9 NA
## 2015-11-11 10 NA
merge(x,y , join = "inner")
## x y
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
merge(x,y , join = "left")
## x y
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
## 2015-11-08 7 NA
## 2015-11-09 8 NA
## 2015-11-10 9 NA
## 2015-11-11 10 NA
merge(x,y , join = "right")
## x y
## 2015-11-02 NA 1
## 2015-11-03 NA 2
## 2015-11-04 NA 3
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
透過 na.locf 可以將 NA值依照最近的出現值填補
na.locf(merge(x,y ))
## x y
## 2015-11-02 NA 1
## 2015-11-03 NA 2
## 2015-11-04 NA 3
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
## 2015-11-08 7 6
## 2015-11-09 8 6
## 2015-11-10 9 6
## 2015-11-11 10 6
na.locf(merge(x,y ), na.rm = TRUE)
## x y
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
## 2015-11-08 7 6
## 2015-11-09 8 6
## 2015-11-10 9 6
## 2015-11-11 10 6
na.locf(merge(x,y ), na.rm = FALSE)
## x y
## 2015-11-02 NA 1
## 2015-11-03 NA 2
## 2015-11-04 NA 3
## 2015-11-05 4 4
## 2015-11-06 5 5
## 2015-11-07 6 6
## 2015-11-08 7 6
## 2015-11-09 8 6
## 2015-11-10 9 6
## 2015-11-11 10 6
透過 first 取得開頭區間的資料
first(matrix_xts)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
first(matrix_xts, "1 week")
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
first(matrix_xts, "2 week")
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
## 2007-01-11 49.88529 50.23910 49.88529 50.23910
## 2007-01-12 50.21258 50.35980 50.17176 50.28519
## 2007-01-13 50.32385 50.48000 50.32385 50.41286
## 2007-01-14 50.46359 50.62395 50.46359 50.60145
first(matrix_xts, 10)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333
## 2007-01-10 49.91228 50.13053 49.91228 49.97246
## 2007-01-11 49.88529 50.23910 49.88529 50.23910
轉換成時間序列格式,繪圖時間座標處理較方便
indexClass(matrix_xts)
## [1] "Date"
#convertIndex(matrix_xts, 'POSIXct')
indexClass(convertIndex(matrix_xts, 'POSIXct'))
## [1] "POSIXct" "POSIXt"
#plot(matrix_xts[,1])
plot(matrix_xts[,1], major.ticks = "months", minor.ticks = FALSE, col=3, main= NULL)
判斷漲跌使用不同顏色
xts_matrix <- as.xts(sample_matrix, descr = "my new xts objct")
xts_data <- xts_matrix["20070101/20070212"]
candlecolors <- ifelse(xts_data[,'Close'] > xts_data[,'Open'], 'RED', 'GREEN')
plot.xts(xts_data,
candle.col=candlecolors,
width=60*60*24*0.25,
main= "Candel plot on xts object",
type="candles")
使用endpoint 取得時間序列資料,指定單位的結束資料,未輸入預設為 month
matrix_xts[endpoints(matrix_xts)]
## Open High Low Close
## 2007-01-31 50.07049 50.22578 50.07049 50.22578
## 2007-02-28 50.69435 50.77091 50.59881 50.77091
## 2007-03-31 48.95616 49.09728 48.95616 48.97490
## 2007-04-30 49.13825 49.33974 49.11500 49.33974
## 2007-05-31 47.82845 47.84044 47.73780 47.73780
## 2007-06-30 47.67468 47.94127 47.67468 47.76719
# 當最後一筆資料 2007/2/12 回傳當月最後一筆資料,而非月底日
xts_data[endpoints(xts_data)]
## Open High Low Close
## 2007-01-31 50.07049 50.22578 50.07049 50.22578
## 2007-02-12 50.88990 50.96653 50.83604 50.96653
matrix_xts[endpoints(matrix_xts, on="weeks")]
## Open High Low Close
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-14 50.46359 50.62395 50.46359 50.60145
## 2007-01-21 50.16188 50.42090 50.16044 50.42090
## 2007-01-28 49.96586 50.00217 49.87468 49.88096
## 2007-02-04 50.48183 50.55509 50.40203 50.55509
## 2007-02-11 50.67849 50.91776 50.67849 50.91160
## 2007-02-18 51.18414 51.32090 51.13713 51.15151
## 2007-02-25 50.78960 50.93187 50.78960 50.84776
## 2007-03-04 50.39273 50.40881 50.24922 50.32636
## 2007-03-11 49.83062 49.88295 49.76031 49.78806
## 2007-03-18 49.29778 49.67857 49.29778 49.65463
## 2007-03-25 48.50649 48.50649 48.33409 48.33973
## 2007-04-01 48.94407 48.97816 48.80962 48.87032
## 2007-04-08 49.54338 49.58968 49.41806 49.41806
## 2007-04-15 49.74707 49.79341 49.66299 49.70942
## 2007-04-22 50.31203 50.33781 50.24788 50.32556
## 2007-04-29 49.30289 49.30289 49.05676 49.13529
## 2007-05-06 49.16606 49.45999 49.16606 49.45999
## 2007-05-13 48.05550 48.05550 47.66209 47.66209
## 2007-05-20 47.56210 47.93085 47.56210 47.93085
## 2007-05-27 48.01018 48.02166 47.90193 47.90193
## 2007-06-03 47.71215 47.71215 47.50198 47.50198
## 2007-06-10 47.74899 47.74899 47.28685 47.28685
## 2007-06-17 47.24783 47.47249 47.24783 47.39521
## 2007-06-24 47.23996 47.30287 47.20932 47.22764
## 2007-06-30 47.67468 47.94127 47.67468 47.76719
# 時間單位為年,會回傳在年度的最後一筆資料 而非年底
matrix_xts[endpoints(matrix_xts, on="years")]
## Open High Low Close
## 2007-06-30 47.67468 47.94127 47.67468 47.76719
透過apply.monthly period.apply ,依照相同時間單位的區間資料運用函式處理
max(xts_matrix["200701"][,4])
## [1] 50.67835
max(xts_matrix["200702"][,4])
## [1] 51.17899
period.apply(matrix_xts[,4], INDEX = endpoints(matrix_xts), FUN=max)
## Close
## 2007-01-31 50.67835
## 2007-02-28 51.17899
## 2007-03-31 50.61559
## 2007-04-30 50.32556
## 2007-05-31 49.58677
## 2007-06-30 47.76719
apply.monthly(matrix_xts[,4], FUN= max)
## Close
## 2007-01-31 50.67835
## 2007-02-28 51.17899
## 2007-03-31 50.61559
## 2007-04-30 50.32556
## 2007-05-31 49.58677
## 2007-06-30 47.76719
apply.weekly(matrix_xts[,4], FUN= max)
## Close
## 2007-01-07 50.39767
## 2007-01-14 50.60145
## 2007-01-21 50.67835
## 2007-01-28 50.24334
## 2007-02-04 50.55509
## 2007-02-11 50.91160
## 2007-02-18 51.15151
## 2007-02-25 51.17899
## 2007-03-04 50.77091
## 2007-03-11 50.29567
## 2007-03-18 49.74033
## 2007-03-25 49.54590
## 2007-04-01 48.97490
## 2007-04-08 49.53819
## 2007-04-15 49.76984
## 2007-04-22 50.32556
## 2007-04-29 49.88539
## 2007-05-06 49.58677
## 2007-05-13 49.18930
## 2007-05-20 47.93085
## 2007-05-27 48.16058
## 2007-06-03 47.87252
## 2007-06-10 47.74770
## 2007-06-17 47.43083
## 2007-06-24 47.67220
## 2007-06-30 47.76719
apply.quarterly(matrix_xts[,4], FUN= max)
## Close
## 2007-03-31 51.17899
## 2007-06-30 50.32556
apply.quarterly(matrix_xts[-180,4], FUN= max)
## Close
## 2007-03-31 51.17899
## 2007-06-29 50.32556
period.max(matrix_xts[,4], INDEX = endpoints(matrix_xts))
## [,1]
## 2007-01-31 50.67835
## 2007-02-28 51.17899
## 2007-03-31 50.61559
## 2007-04-30 50.32556
## 2007-05-31 49.58677
## 2007-06-30 47.76719
透過split將時間序列資料 依時間單位分群,預設以月為endpoint分群
set.seed(9)
xts(runif(9), order.by = as.Date(paste("2015-0", 1:9,"-01", sep="")))
## [,1]
## 2015-01-01 0.22160140
## 2015-02-01 0.02423391
## 2015-03-01 0.20711902
## 2015-04-01 0.21573355
## 2015-05-01 0.44372359
## 2015-06-01 0.13407615
## 2015-07-01 0.39065779
## 2015-08-01 0.36931577
## 2015-09-01 0.66869296
x <- as.xts(sample_matrix)
x.s.1 <- split(x)
x.s.2 <- split(x, f = "months")
head(x.s.2[[1]],3)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
tail(x.s.2[[1]],3)
## Open High Low Close
## 2007-01-29 49.85624 49.93038 49.76308 49.91875
## 2007-01-30 49.85477 50.02180 49.77242 50.02180
## 2007-01-31 50.07049 50.22578 50.07049 50.22578
每兩季分群
x.s.3 <- split(x, f = "quarters")
head(x.s.3[[1]],3)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
tail(x.s.3[[1]],3)
## Open High Low Close
## 2007-03-29 48.59236 48.69988 48.57432 48.69988
## 2007-03-30 48.74562 49.00218 48.74562 48.93546
## 2007-03-31 48.95616 49.09728 48.95616 48.97490
每兩週分群
x.s.4 <- split(x, f = "weeks", k=2)
head(x.s.4[[1]],3)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
tail(x.s.4[[1]],3)
## Open High Low Close
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
每兩個月分群
x.s.5 <- split(x, f = "months", k=2)
head(x.s.5[[1]],3)
## Open High Low Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
tail(x.s.5[[1]],3)
## Open High Low Close
## 2007-02-26 50.88168 50.88168 50.75481 50.75481
## 2007-02-27 50.74333 50.78909 50.61874 50.69206
## 2007-02-28 50.69435 50.77091 50.59881 50.77091
xtsAttribute
x <- xts(matrix(1:(9*6), nc = 6), order.by = as.Date(13000, origin="1970-01-01")+1:9,
a1="my attribute")
as.Date(13000)
## [1] "2005-08-05"
xtsAttributes(x)
## $a1
## [1] "my attribute"
attributes(x)
## $dim
## [1] 9 6
##
## $index
## [1] 1123286400 1123372800 1123459200 1123545600 1123632000 1123718400
## [7] 1123804800 1123891200 1123977600
## attr(,"tzone")
## [1] "UTC"
## attr(,"tclass")
## [1] "Date"
##
## $class
## [1] "xts" "zoo"
##
## $.indexCLASS
## [1] "Date"
##
## $tclass
## [1] "Date"
##
## $.indexTZ
## [1] "UTC"
##
## $tzone
## [1] "UTC"
##
## $a1
## [1] "my attribute"
xtsAttributes(x) <- list(a2=2020)
xtsAttributes(x)
## $a1
## [1] "my attribute"
##
## $a2
## [1] 2020
xtsAttributes(x) <- list(a1=NULL)
xtsAttributes(x)
## $a2
## [1] 2020
依年月取得 firstof, last of
firstof(2000)
## [1] "2000-01-01 CST"
firstof("2000")
## [1] "2000-01-01 CST"
firstof("2000-6")
## [1] "2000-06-01 01:00:00 CST"
firstof(year=2000, month=6)
## [1] "2000-06-01 CST"
firstof(2000,6)
## [1] "2000-06-01 CST"
lastof("2000-06")
## [1] NA
lastof(year=2000, month=6)
## [1] "2000-06-30 23:59:59 CST"
str(lastof(year=2000, month=6))
## POSIXct[1:1], format: "2000-06-30 23:59:59"
align.time,時間單位為秒,將時間依照單位切齊
y <- Sys.time() + 1:3000
#切齊每10秒為一個時間單位
head(align.time(y, 10),20)
## [1] "2015-11-01 23:25:10 CST" "2015-11-01 23:25:10 CST"
## [3] "2015-11-01 23:25:10 CST" "2015-11-01 23:25:10 CST"
## [5] "2015-11-01 23:25:10 CST" "2015-11-01 23:25:20 CST"
## [7] "2015-11-01 23:25:20 CST" "2015-11-01 23:25:20 CST"
## [9] "2015-11-01 23:25:20 CST" "2015-11-01 23:25:20 CST"
## [11] "2015-11-01 23:25:20 CST" "2015-11-01 23:25:20 CST"
## [13] "2015-11-01 23:25:20 CST" "2015-11-01 23:25:20 CST"
## [15] "2015-11-01 23:25:20 CST" "2015-11-01 23:25:30 CST"
## [17] "2015-11-01 23:25:30 CST" "2015-11-01 23:25:30 CST"
## [19] "2015-11-01 23:25:30 CST" "2015-11-01 23:25:30 CST"
#切齊每一分鐘為一個時間單位
head(align.time(y, 60),60)
## [1] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [3] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [5] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [7] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [9] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [11] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [13] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [15] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [17] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [19] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [21] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [23] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [25] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [27] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [29] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [31] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [33] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [35] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [37] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [39] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [41] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [43] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [45] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [47] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [49] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [51] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [53] "2015-11-01 23:26:00 CST" "2015-11-01 23:26:00 CST"
## [55] "2015-11-01 23:26:00 CST" "2015-11-01 23:27:00 CST"
## [57] "2015-11-01 23:27:00 CST" "2015-11-01 23:27:00 CST"
## [59] "2015-11-01 23:27:00 CST" "2015-11-01 23:27:00 CST"