在R中將data.table 轉化爲zoo時間格式

解決辦法是從stackoverflow上面看到,我來翻譯成中文,並加上實際的例子應用。數據結構

建立data.table

library(data.table)
library(zoo)
dt <- data.table(
  mydates = as.Date("2012-01-01") + 1:9, 
  value1 = sort(rpois(9, 6)),
  value2 = sort(rpois(9, 6)),
  value3 = sort(rpois(9, 6)),
  value4 = sort(rpois(9, 6)),
  value5 = sort(rpois(9, 6)))

#查看數據結構
> dt
      mydates value1 value2 value3 value4 value5
1: 2012-01-02      4      4      2      2      1
2: 2012-01-03      4      4      3      5      4
3: 2012-01-04      4      4      4      5      5
4: 2012-01-05      5      5      4      5      5
5: 2012-01-06      5      5      5      6      5
6: 2012-01-07      7      6      7      6      6
7: 2012-01-08      8      8      9      7      8
8: 2012-01-09     10      8      9      9      9
9: 2012-01-10     13      9     10     10     11
> str(dt)
Classes ‘data.table’ and 'data.frame':    9 obs. of  6 variables:
 $ mydates: Date, format: "2012-01-02" "2012-01-03" ...
 $ value1 : int  4 4 4 5 5 7 8 10 13
 $ value2 : int  4 4 4 5 5 6 8 8 9
 $ value3 : int  2 3 4 4 5 7 9 9 10
 $ value4 : int  2 5 5 5 6 6 7 9 10
 $ value5 : int  1 4 5 5 5 6 8 9 11
 - attr(*, ".internal.selfref")=<externalptr>

轉化爲zoo格式,提取特定日期數據

基本用法:函數

zoo(x,index(x))
window(zoo_object, start = as.Date("2003-02-01"), end = as.Date("2003-03-01"))`

這裏須要注意的是x爲向量、矩陣和 轉化才能轉化爲zoo;而window函數的操做對象必須是zoo格式的.
參考data.table的用法 x[i, j, by, keyby, with = TRUE, ...],默認列是被當作變量,可是經過with=FALSE能夠變成一列向量,用於動態選取列,可是返回結果仍是data.table的格式。lua

x[i, j, by, keyby, with = TRUE, ...], By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. When with=FALSE j is a character vector of column names or a numeric vector of column positions to select, and the value returned is always a data.table. with=FALSE is often useful in data.table to select columns dynamically.翻譯

zooObj <- zoo(dt[,-1,with=FALSE],dt$times)
window(zooObj,start = as.Date("2012-01-05"), end = as.Date("2012-01-08"))

這樣就能夠提取特定日期內的數據了。可是,須要注意的是index必須是惟一的,這裏的dt$times要惟一才能順利運行。若是數據量大的話,若是粒度能到秒就ok了;或者groupby 一下,而後再做爲索引。
關於這個問題,我尚未想好解決方案,待補充。code

相關文章
相關標籤/搜索