時間序列基礎包xts_r語言

來源:互聯網
上載者:User

參考書目: 《R的極客理想——工具篇 》 xts介紹

xts是對時間序列資料(zoo)的一種擴充實現,目標是為了統一時間序列的操作介面。實際上,xts類型繼承了zoo類型,豐富了時間序列資料處理的函數,API定義更貼近使用者,更實用. xts資料結構

xts擴充zoo的基礎結構,由3部分組成,如圖2-7所示。

索引部分:時間類型向量。

資料部分:以矩陣為基礎類型,支援可以與矩陣相互轉換的任何類型。

屬性部分:附件資訊,包括時區和索引時間類型的格式等。 xtsAPI介紹

(1)xts基礎

xts: 定義xts資料類型,繼承zoo類型。

coredata.xts: 查看或編輯xts對象的資料部分。

xtsAttributes: 查看或編輯xts對象的屬性部分。

dimnames.xts: 查看或編輯xts維度名。

sample_matrix: 測試資料集,包括180條xts對象的記錄,matrix類型。

xtsAPI: C語言API介面。

(2)類型轉換

as.xts: 轉換對象到xts(zoo)類型。

as.xts.methods: 轉換對象到xts函數。

plot.xts: 為plot函數提供xts的介面作圖。

.parseISO8601: 把字串(ISO8601格式)輸出為POSIXct類型的,包括開始時間和結束時間的list對象。

firstof: 建立一個開始時間,POSIXct類型。

lastof: 建立一個結束時間,POSIXct類型。

indexClass: 取索引類型。

.indexDate: 索引的日期。

.indexday: 索引的日期,同.indexDate。

.indexyday: 索引的年(日)值。

.indexmday: 索引的月(日)值。

.indexwday: 索引的周(日)值。

.indexweek: 索引的周值。

.indexmon: 索引的月值。

.indexyear: 索引的年值。

.indexhour: 索引的時值。

.indexmin: 索引的分值。

.indexsec: 索引的秒值。

(3)資料處理

align.time: 以下一個時間對齊資料,秒,分鐘,小時。

endpoints: 按時間單元提取索引資料。

merge.xts: 合并多個xts對象,重寫zoo::merge.zoo函數。

rbind.xts: 資料按行合并,為rbind函數提供xts的介面。

split.xts: 資料分割,為split函數,提供xts的介面。

na.locf.xts: 替換NA值,重寫zoo:na.locf函數。

(4)資料統計

apply.daily: 按日分割資料,執行函數。

apply.weekly: 按周分割資料,執行函數。

apply.monthly: 按月分割資料,執行函數。

apply.quarterly: 按季分割資料,執行函數。

apply.yearly: 按年分割資料,執行函數。

to.period: 按期間分割資料。

period.apply: 按期間執行自訂函數。

period.max: 按期間計算最大值。

period.min: 按期間計算最小值。

period.prod: 按期間計算指數。

period.sum: 按期間求和。

nseconds: 計算資料集包括多少秒。

nminutes: 計算資料集包括多少分。

nhours: 計算資料集包括多少時。

ndays: 計算資料集包括多少日。

nweeks: 計算資料集包括多少周。

nmonths: 計算資料集包括多少月。

nquarters: 計算資料集包括多少季。

nyears: 計算資料集包括多少年。

periodicity: 查看時間序列的期間。

(5)協助工具輔助

first: 從開始到結束設定條件取子集。

last: 從結束到開始設定條件取子集。

timeBased: 判斷是否是時間類型。

timeBasedSeq: 建立時間的序列。

diff.xts: 計算步長和差分。

isOrdered: 檢查向量是否是順序的。

make.index.unique: 強制時間唯一,增加毫秒隨機數。

axTicksByTime: 計算X軸刻度標記位置按時間描述。

indexTZ: 查詢xts對象的時區。
#xts使用
##基本操作

> sample.xts <- as.xts(sample_matrix, descr='my new xts object')                       # 建立一個xts對象,並設定屬性descr  > class(sample.xts)  # xts是繼承zoo類型的對象  [1] "xts" "zoo"  > str(sample.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 1   $ descr: chr "my new xts object"  #描述> attr(sample.xts,'descr')  # 查看對象的屬性descr  [1] "my new xts object" 
在[]中,通過字串匹配進行xts資料查詢。
> head(sample.xts['2007'])   # 選出2007年的資料                 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  > head(sample.xts['2007-03/'])  # 選出2007年03月的資料                 Open     High      Low    Close  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  > head(sample.xts['2007-03-06/2007'])  # 選出2007年03月06日到2007年的資料                 Open     High      Low    Close  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  > sample.xts['2007-01-03']  # 選出2007年01月03日的資料                Open     High     Low    Close 
用xts對象畫圖

用xts對象可以畫曲線圖和K線圖,下面是產生這兩種圖的代碼,首先是曲線圖:

> data(sample_matrix)  > plot(as.xts(sample_matrix))  Warning message:  In plot.xts(as.xts(sample_matrix)) :    only the univariate series will be plotted

警告資訊提示,只有單變數序列將被繪製,即只畫出第一列資料sample_matrix[,1]的曲線。

k線圖

 plot(as.xts(sample_matrix), type='candles')  #畫K線圖


Better financial plots can be found in the quantmod package, though these are generally incompatible with standard R graphics tools. xts對象的類型轉換 建立首尾時間函數firstof()和lastof()

> firstof(2000)  # 2000年的第一天,時分秒顯示省略  [1] "2000-01-01 CST"  > firstof(2005,01,01)  [1] "2005-01-01 CST"  > lastof(2007)  # 2007年的最後一天,最後一秒  [1] "2007-12-31 23:59:59.99998 CST"  > lastof(2007,10)  [1] "2007-10-31 23:59:59.99998 CST" > .parseISO8601('2000')  # 以ISO8601格式,建立2000年首尾時間  $first.time  [1] "2000-01-01 CST"  $last.time  [1] "2000-12-31 23:59:59.99998 CST"  > .parseISO8601('2000-05/2001-02')  # 以ISO8601格式,建立2000年05月開始,2001年02月結束的時間  $first.time  [1] "2000-05-01 CST"  $last.time  [1] "2001-02-28 23:59:59.99998 CST"  > .parseISO8601('2000-01/02')  $first.time  [1] "2000-01-01 CST"  $last.time  [1] "2000-02-29 23:59:59.99998 CST"  > .parseISO8601('T08:30/T15:00')  $first.time  [1] "1970-01-01 08:30:00 CST"  $last.time  [1] "1970-12-31 15:00:59.99999 CST" 
建立以時間類型為索引的xts對象
> x <- timeBasedSeq('2010-01-01/2010-01-02 12:00')  # 建立POSIXt類型時間  > head(x)  [1] "2010-01-01 00:00:00 CST"  [2] "2010-01-01 00:01:00 CST"  [3] "2010-01-01 00:02:00 CST"  [4] "2010-01-01 00:03:00 CST"  [5] "2010-01-01 00:04:00 CST"  [6] "2010-01-01 00:05:00 CST"  > class(x)  [1] "POSIXt"  "POSIXct"  > x <- xts(1:length(x), x)   # 以時間為索引建立xts對象  > head(x)                      [,1]  2010-01-01 00:00:00    1  2010-01-01 00:01:00    2  2010-01-01 00:02:00    3  2010-01-01 00:03:00    4  2010-01-01 00:04:00    5  2010-01-01 00:05:00    6  > indexClass(x)  [1] "POSIXt"  "POSIXct"
格式化索引時間的顯示
> indexFormat(x) <- "%Y-%b-%d %H:%M:%OS3"  # 通過正則格式化索引的時間顯示  > head(x)                            [,1]  2010-一月-01 00:00:00.000    1  2010-一月-01 00:01:00.000    2  2010-一月-01 00:02:00.000    3  2010-一月-01 00:03:00.000    4  2010-一月-01 00:04:00.000    5  2010-一月-01 00:05:00.000    6 
查看索引時間
> .indexhour(head(x))  # 按小時取索引時間  [1] 0 0 0 0 0 0  > .indexmin(head(x))   # 按分鐘取索引時間  [1] 0 1 2 3 4 5 
xts對象的資料處理 資料對齊
x <- Sys.time() + 1:30  > align.time(x, 10)    #整10秒對齊,秒位為10的整數倍   [1] "2013-11-18 15:42:30 CST" "2013-11-18 15:42:30 CST"   [3] "2013-11-18 15:42:30 CST" "2013-11-18 15:42:40 CST"   [5] "2013-11-18 15:42:40 CST" "2013-11-18 15:42:40 CST"   [7] "2013-11-18 15:42:40 CST" "2013-11-18 15:42:40 CST"   [29] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"  > align.time(x, 60)    #整60秒對齊,秒位為0,分位為整數   [1] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"   [3] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"   [5] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"   [7] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"   [9] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST"  [11] "2013-11-18 15:43:00 CST" "2013-11-18 15:43:00 CST" 
按時間分割資料,並計算
xts.ts <- xts(rnorm(231),as.Date(13514:13744,origin="1970-01-01"))  > apply.monthly(xts.ts,mean)  # 按月計算均值,以每月的最後一日顯示                    [,1]  2007-01-31  0.17699984  2007-02-28  0.30734220  2007-03-31 -0.08757189  2007-04-30  0.18734688  2007-05-31  0.04496954  2007-06-30  0.06884836  2007-07-31  0.25081814  2007-08-19 -0.28845938  > apply.monthly(xts.ts,function(x) var(x))  # 按月計算自訂函數(方差),以每月的最後一日顯示                  [,1]  2007-01-31 0.9533217  2007-02-28 0.9158947  2007-03-31 1.2821450  2007-04-30 1.2805976  2007-05-31 0.9725438  2007-06-30 1.5228904  2007-07-31 0.8737030  2007-08-19 0.8490521  > apply.quarterly(xts.ts,mean)  # 按季計算均值,以每季的最後一日顯示                   [,1]  2007-03-31 0.12642053  2007-06-30 0.09977926  2007-08-19 0.04589268  > apply.yearly(xts.ts,mean)  # 按年計算均值,以年季的最後一日顯示                   [,1]  2007-08-19 0.09849522 
使用to.period()函數按間隔分割資料
> data(sample_matrix)  > to.period(sample_matrix)  # 預設按月分割矩陣資料             sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close  2007-01-31           50.03978           50.77336          49.76308            50.22578  2007-02-28           50.22448           51.32342          50.19101            50.77091  2007-03-31           50.81620           50.81620          48.23648            48.97490  2007-04-30           48.94407           50.33781          48.80962            49.33974  2007-05-31           49.34572           49.69097          47.51796            47.73780  2007-06-30           47.74432           47.94127          47.09144            47.76719  > class(to.period(sample_matrix))  [1] "matrix"  > samplexts <- as.xts(sample_matrix)  # 預設按月分割xts類型資料  > to.period(samplexts)             samplexts.Open samplexts.High samplexts.Low samplexts.Close  2007-01-31       50.03978       50.77336      49.76308        50.22578  2007-02-28       50.22448       51.32342      50.19101        50.77091  2007-03-31       50.81620       50.81620      48.23648        48.97490  2007-04-30       48.94407       50.33781      48.80962        49.33974  2007-05-31       49.34572       49.69097      47.51796        47.73780  2007-06-30       47.74432       47.94127      47.09144        47.76719  > class(to.period(samplexts))  [1] "xts" "zoo" 
使用endpoints()函數,按間隔分割索引資料
> data(sample_matrix)  > endpoints(sample_matrix)  # 預設按月分割  [1]   0  30  58  89 119 150 180  > endpoints(sample_matrix, 'days',k=7)  # 按每7日分割   [1]   0   6  13  20  27  34  41  48  55  62  69  76  83  90  97 104 111 118 125  [20] 132 139 146 153 160 167 174 180  > endpoints(sample_matrix, 'weeks')     # 按周分割   [1]   0   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119 126  [20] 133 140 147 154 161 168 175 180  > endpoints(sample_matrix, 'months')    # 按月分割  [1]   0  30  58  89 119 150 180 
使用merge()函數進行資料合併,按列合并
> (x <- xts(4:10, Sys.Date()+4:10))     # 建立2個xts資料集             [,1]  2013-11-22    4  2013-11-23    5  2013-11-24    6  2013-11-25    7  2013-11-26    8  2013-11-27    9  2013-11-28   10  > (y <- xts(1:6, Sys.Date()+1:6))             [,1]  2013-11-19    1  2013-11-20    2  2013-11-21    3  2013-11-22    4  2013-11-23    5  2013-11-24    6  > merge(x,y)  # 按列合并資料,空項以NA填空              x  y  2013-11-19 NA  1  2013-11-20 NA  2  2013-11-21 NA  3  2013-11-22  4  4  2013-11-23  5  5  2013-11-24  6  6  2013-11-25  7 NA  2013-11-26  8 NA  2013-11-27  9 NA  2013-11-28 10 NA  > merge(x,y, join='inner')  #按索引合并資料             x y  2013-11-22 4 4  2013-11-23 5 5  2013-11-24 6 6  > merge(x,y, join='left')  #以左側為基礎合并資料              x  y  2013-11-22  4  4  2013-11-23  5  5  2013-11-24  6  6  2013-11-25  7 NA  2013-11-26  8 NA  2013-11-27  9 NA  2013-11-28 10 NA 
使用split()函數切片,按行切片
> data(sample_matrix)  > x <- as.xts(sample_matrix)  > split(x)[[1]]  # 預設按月進行切片,列印第一個月的資料                 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  > split(x, f="weeks")[[1]]  # 按周切片,列印前1周資料                 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  
NA值處理
> x <- xts(1:10, Sys.Date()+1:10)  > x[c(1,2,5,9,10)] <- NA  > x             [,1]  2013-11-19   NA  2013-11-20   NA  2013-11-21    3  2013-11-22    4  2013-11-23   NA  2013-11-24    6  2013-11-25    7  2013-11-26    8  2013-11-27   NA  2013-11-28   NA  > na.locf(x)  #取NA的前一個,替換NA值             [,1]  2013-11-19   NA  2013-11-20   NA  2013-11-21    3  2013-11-22    4  2013-11-23    4  2013-11-24    6  2013-11-25    7  2013-11-26    8  2013-11-27    8  2013-11-28    8  > na.locf(x, fromLast=TRUE)  #取NA後一個,替換NA值             [,1]  2013-11-19    3  2013-11-20    3  2013-11-21    3  2013-11-22    4  2013-11-23    6  2013-11-24    6  2013-11-25    7  2013-11-26    8  2013-11-27   NA  2013-11-28   NA 
xts對象的資料統計計算 取xts對象的開始時間和結束時間
> xts.ts <- xts(rnorm(231),as.Date(13514:13744,origin="1970-01-01"))  > start(xts.ts)  # 取開始時間  [1] "2007-01-01"  > end(xts.ts)  # 取結束時間  [1] "2007-08-19"  > periodicity(xts.ts)  # 以日為單位,列印開始和結束時間  Daily periodicity from 2007-01-01 to 2007-08-19
計算時間區間
> data(sample_matrix)  > ndays(sample_matrix)  # 計算資料有多少日  [1] 180  > nweeks(sample_matrix)  # 計算資料有多少周  [1] 26  > nmonths(sample_matrix)  # 計算資料有多少月  [1] 6  > nquarters(sample_matrix)  # 計算資料有多少季  [1] 2  > nyears(sample_matrix)  # 計算資料有多少年  [1] 1 
按期間計算統計指標
> zoo.data <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))  > ep <- endpoints(zoo.data,'weeks')  # 按周獲得期間索引  > ep   [1]   0   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119  [19] 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231  > period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x))  # 計算周的均值  2007-01-07 2007-01-14 2007-01-21 2007-01-28 2007-02-04 2007-02-11 2007-02-18   10.200488   9.649387  10.304151   9.864847  10.382943   9.660175   9.857894  2007-02-25 2007-03-04 2007-03-11 2007-03-18 2007-03-25 2007-04-01 2007-04-08   10.495037   9.569531  10.292899   9.651616  10.089103   9.961048  10.304860  2007-04-15 2007-04-22 2007-04-29 2007-05-06 2007-05-13 2007-05-20 2007-05-27    9.658432   9.887531  10.608082   9.747787  10.052955   9.625730  10.430030  2007-06-03 2007-06-10 2007-06-17 2007-06-24 2007-07-01 2007-07-08 2007-07-15    9.814703  10.224869   9.509881  10.187905  10.229310  10.261725   9.855776  2007-07-22 2007-07-29 2007-08-05 2007-08-12 2007-08-19    9.445072  10.482020   9.844531  10.200488   9.649387  > head(period.max(zoo.data, INDEX=ep))  # 計算周的最大值                 [,1]  2007-01-07 12.05912  2007-01-14 10.79286  2007-01-21 11.60658  2007-01-28 11.63455  2007-02-04 12.05912  2007-02-11 10.67887  > head(period.min(zoo.data, INDEX=ep))  # 計算周的最小值                 [,1]  2007-01-07 8.874509  2007-01-14 8.534655  2007-01-21 9.069773  2007-01-28 8.461555  2007-02-04 9.421085  2007-02-11 8.534655  > head(period.prod(zoo.data, INDEX=ep)) # 計算周的一個指數值                 [,1]  2007-01-07 11140398  2007-01-14  7582350  2007-01-21 11930334  2007-01-28  8658933  2007-02-04 12702505  2007-02-11  7702767 
xts對象的時間序列操作 檢查時間類型
> class(Sys.time());timeBased(Sys.time())  # Sys.time() 是時間類型POSIXct  [1] "POSIXct" "POSIXt"  [1] TRUE  > class(Sys.Date());timeBased(Sys.Date())  # Sys.Date() 是時間類型Date  [1] "Date"  [1] TRUE  > class(20070101);timeBased(20070101)  # 20070101 不是時間類型  [1] "numeric"  [1] FALSE
使用timeBasedSeq()函數建立時間序列
> timeBasedSeq('1999/2008')   # 按年   [1] "1999-01-01" "2000-01-01" "2001-01-01" "2002-01-01" "2003-01-01"   [6] "2004-01-01" "2005-01-01" "2006-01-01" "2007-01-01" "2008-01-01"  > head(timeBasedSeq('199901/2008'))  # 按月  [1] "十二月 1998" "一月 1999"   "二月 1999"   "三月 1999"   "四月 1999"  [6] "五月 1999"  > head(timeBasedSeq('199901/2008/d'),40)  # 按日   [1] "十二月 1998" "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   [6] "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"  [11] "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"  [16] "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"  [21] "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"  [26] "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"   "一月 1999"  [31] "一月 1999"   "一月 1999"   "二月 1999"   "二月 1999"   "二月 1999"  [36] "二月 1999"   "二月 1999"   "二月 1999"   "二月 1999"   "二月 1999"  > timeBasedSeq('20080101 0830',length=100)  # 按數量建立,100分鐘的資料集  $from  [1] "2008-01-01 08:30:00 CST"  $to  [1] NA  $by  [1] "mins"  $length.out  [1] 100
按索引取資料first()和last()
> x <- xts(1:100, Sys.Date()+1:100)  > head(x)             [,1]  2013-11-19    1  2013-11-20    2  2013-11-21    3  2013-11-22    4  2013-11-23    5  2013-11-24    6  > first(x, 10)  # 取前10條資料             [,1]  2013-11-19    1  2013-11-20    2  2013-11-21    3  2013-11-22    4  2013-11-23    5  2013-11-24    6  2013-11-25    7  2013-11-26    8  2013-11-27    9  2013-11-28   10  > first(x, '1 day')    # 取1天的資料             [,1]  2013-11-19    1  > last(x, '1 weeks')  # 取最後1周的資料             [,1]  2014-02-24   98  2014-02-25   99  2014-02-26  100
計算步長lag()和差分diff()
> x <- xts(1:5, Sys.Date()+1:5)  > lag(x)  # 以1為步長             [,1]  2013-11-19   NA  2013-11-20    1  2013-11-21    2  2013-11-22    3  2013-11-23    4  > lag(x, k=-1, na.pad=FALSE)  # 以-1為步長,並去掉NA值             [,1]  2013-11-19    2  2013-11-20    3  2013-11-21    4  2013-11-22    5  > diff(x)   # 1階差分             [,1]  2013-11-19   NA  2013-11-20    1  2013-11-21    1  2013-11-22    1  2013-11-23    1  > diff(x, lag=2)  # 2階差分             [,1]  2013-11-19   NA  2013-11-20   NA  2013-11-21    2  2013-11-22    2  2013-11-23    2 
使用isOrdered()函數,檢查向量是否排序好的
> isOrdered(1:10, increasing=TRUE)  [1] TRUE  > isOrdered(1:10, increasing=FALSE)  [1] FALSE  > isOrdered(c(1,1:10), increasing=TRUE)  [1] FALSE  > isOrdered(c(1,1:10), increasing=TRUE, strictly=FALSE)  [1] TRUE 
使用make.index.unique()函數,強制唯一索引
> x <- xts(1:5, as.POSIXct("2011-01-21") + c(1,1,1,2,3)/1e3)  > x                          [,1]  2011-01-21 00:00:00.000    1  2011-01-21 00:00:00.000    2  2011-01-21 00:00:00.000    3  2011-01-21 00:00:00.002    4  2011-01-21 00:00:00.003    5  > make.index.unique(x)  # 增加毫秒級精度,保證索引的唯一性                             [,1]  2011-01-21 00:00:00.000999    1  2011-01-21 00:00:00.001000    2  2011-01-21 00:00:00.001001    3  2011-01-21 00:00:00.002000    4  2011-01-21 00:00:00.003000    5
查詢xts對象時區
> x <- xts(1:10, Sys.Date()+1:10)  > indexTZ(x)  # 時區查詢  [1] "UTC"  > tzone(x)  [1] "UTC"  > str(x)  An 'xts' object on 2013-11-19/2013-11-28 containing:    Data: int [1:10, 1] 1 2 3 4 5 6 7 8 9 10    Indexed by objects of class: [Date] TZ: UTC    xts Attributes:   NULL 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.