[英]How to create multiple xts objects from one xts object
我已經從數據框中創建了一個“ xts”對象-該數據框是從“ csv”文件中加載的。
'xts'對象看起來像這樣:-
entitycode,usage
2016-01-01 1,16521
2016-01-01 2,6589
2016-01-02 1,16540
2016-01-02 2,6687
2016-01-03 1,16269
2016-01-03 2,6642
其中總共有1462條記錄-從01/01/2016到2017年12月31日,每個實體代碼1和2分別有731條記錄,頻率為1天。
實體代碼1和2指的是不同的區域,例如“ region1”和“ region2”。
有沒有一種方法可以為實體代碼1和2(或“ region1”和“ region2”)創建單獨的“ xts”對象(變量),每個實體代碼有731行,名稱類似於“ region1_xts”和“ region1_xts”?
最好的祝福
迪帕克
我會建議split
婷xts
導致對象list
的xts
對象
split(xts, xts$entitycode)
#$`1`
# entitycode usage
#2016-01-01 1 16521
#2016-01-02 1 16540
#2016-01-03 1 16269
#
#$`2`
# entitycode usage
#2016-01-01 2 6589
#2016-01-02 2 6687
#2016-01-03 2 6642
然后,您可以使用*apply
系列的功能輕松地對不同的list
元素(即xts
對象)進行操作。
df <- read.csv(text =
" date,entitycode,usage
2016-01-01, 1,16521
2016-01-01, 2,6589
2016-01-02, 1,16540
2016-01-02, 2,6687
2016-01-03, 1,16269
2016-01-03, 2,6642", header = T)
mat <- as.matrix(df[, -1])
rownames(mat) <- df[, 1]
colnames(mat) <- colnames(df)[-1]
xts <- as.xts(mat)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.