2015-07-20 66 views
1

我試圖將季度時間序列轉換爲data.frame。將季度時間序列(類ts)轉換爲data.frame

我的時間序列的dput

dput(res$oenb_dependent) 
structure(c(1.02270390000001, -5.06831440000002, 0.665771299999989, 
3.31613740000003, -2.15867040000001, -0.783362300000022, -0.22032089999999, 
2.416144, -1.76254059999999, -0.156503700000002, -7.9803936, 
9.45947149999999, -4.8104584, 8.48271070000001, -6.18952620000002, 
1.42885950000002, 1.4896459, -0.419852200000008, -5.1583964, 
5.25022940000001, 1.0567102, -1.09233420000001, -1.58522979999999, 
0.606193599999997, -0.375233499999993, 2.50086640000001, -1.39997290000002, 
2.28021660000002, -2.1468756, -1.4890328, -0.792543760000001, 
3.21804705000001, -0.944078860000019, -0.278023159999989, -0.207530789999993, 
-1.12610048000001, 2.0883735, -0.742485400000007, 0.442037290000002, 
-1.48905938, 1.39644423999999, -3.89173769999999, 11.25665848, 
-9.22884035, 3.26856762, -0.00179540999998551, -2.39664325000003, 
4.00455574000001, -5.60891295, 4.6556348, -4.40536951, 6.64234497, 
-7.34787318999999, 7.56303005999999, -8.23083674, 4.43247855, 
1.31090412), .Tsp = c(2000.75, 2014.75, 4), class = "ts") 

res$oenb_dependent 
      Qtr1  Qtr2  Qtr3  Qtr4 
2000          1.02270390 
2001 -5.06831440 0.66577130 3.31613740 -2.15867040 
2002 -0.78336230 -0.22032090 2.41614400 -1.76254060 
2003 -0.15650370 -7.98039360 9.45947150 -4.81045840 
2004 8.48271070 -6.18952620 1.42885950 1.48964590 
2005 -0.41985220 -5.15839640 5.25022940 1.05671020 
2006 -1.09233420 -1.58522980 0.60619360 -0.37523350 
2007 2.50086640 -1.39997290 2.28021660 -2.14687560 
2008 -1.48903280 -0.79254376 3.21804705 -0.94407886 
2009 -0.27802316 -0.20753079 -1.12610048 2.08837350 
2010 -0.74248540 0.44203729 -1.48905938 1.39644424 
2011 -3.89173770 11.25665848 -9.22884035 3.26856762 
2012 -0.00179541 -2.39664325 4.00455574 -5.60891295 
2013 4.65563480 -4.40536951 6.64234497 -7.34787319 
2014 7.56303006 -8.23083674 4.43247855 1.31090412 

str(res$oenb_dependent) 
# Time-Series [1:57] from 2001 to 2015: 1.023 -5.068 0.666 3.316 -2.159 ... 

這裏是我試圖時間序列轉換成數據幀代碼:

dmn <- list(c("Qtr1", "Qtr2", "Qtr3", "Qtr4"), unique(floor(time(res[1])))) 
as.data.frame(t(matrix(res[1], 4, dimnames = dmn))) 
                                                                                                                                                                                     Qtr1 
1 1.02270390, -5.06831440, 0.66577130, 3.31613740, -2.15867040, -0.78336230, -0.22032090, 2.41614400, -1.76254060, -0.15650370, -7.98039360, 9.45947150, -4.81045840, 8.48271070, -6.18952620, 1.42885950, 1.48964590, -0.41985220, -5.15839640, 5.25022940, 1.05671020, -1.09233420, -1.58522980, 0.60619360, -0.37523350, 2.50086640, -1.39997290, 2.28021660, -2.14687560, -1.48903280, -0.79254376, 3.21804705, -0.94407886, -0.27802316, -0.20753079, -1.12610048, 2.08837350, -0.74248540, 0.44203729, -1.48905938, 1.39644424, -3.89173770, 11.25665848, -9.22884035, 3.26856762, -0.00179541, -2.39664325, 4.00455574, -5.60891295, 4.65563480, -4.40536951, 6.64234497, -7.34787319, 7.56303006, -8.23083674, 4.43247855, 1.31090412 
                                                                                                                                                                                     Qtr2 
1 1.02270390, -5.06831440, 0.66577130, 3.31613740, -2.15867040, -0.78336230, -0.22032090, 2.41614400, -1.76254060, -0.15650370, -7.98039360, 9.45947150, -4.81045840, 8.48271070, -6.18952620, 1.42885950, 1.48964590, -0.41985220, -5.15839640, 5.25022940, 1.05671020, -1.09233420, -1.58522980, 0.60619360, -0.37523350, 2.50086640, -1.39997290, 2.28021660, -2.14687560, -1.48903280, -0.79254376, 3.21804705, -0.94407886, -0.27802316, -0.20753079, -1.12610048, 2.08837350, -0.74248540, 0.44203729, -1.48905938, 1.39644424, -3.89173770, 11.25665848, -9.22884035, 3.26856762, -0.00179541, -2.39664325, 4.00455574, -5.60891295, 4.65563480, -4.40536951, 6.64234497, -7.34787319, 7.56303006, -8.23083674, 4.43247855, 1.31090412 
                                                                                                                                                                                     Qtr3 
1 1.02270390, -5.06831440, 0.66577130, 3.31613740, -2.15867040, -0.78336230, -0.22032090, 2.41614400, -1.76254060, -0.15650370, -7.98039360, 9.45947150, -4.81045840, 8.48271070, -6.18952620, 1.42885950, 1.48964590, -0.41985220, -5.15839640, 5.25022940, 1.05671020, -1.09233420, -1.58522980, 0.60619360, -0.37523350, 2.50086640, -1.39997290, 2.28021660, -2.14687560, -1.48903280, -0.79254376, 3.21804705, -0.94407886, -0.27802316, -0.20753079, -1.12610048, 2.08837350, -0.74248540, 0.44203729, -1.48905938, 1.39644424, -3.89173770, 11.25665848, -9.22884035, 3.26856762, -0.00179541, -2.39664325, 4.00455574, -5.60891295, 4.65563480, -4.40536951, 6.64234497, -7.34787319, 7.56303006, -8.23083674, 4.43247855, 1.31090412 
                                                                                                                                                                                     Qtr4 
1 1.02270390, -5.06831440, 0.66577130, 3.31613740, -2.15867040, -0.78336230, -0.22032090, 2.41614400, -1.76254060, -0.15650370, -7.98039360, 9.45947150, -4.81045840, 8.48271070, -6.18952620, 1.42885950, 1.48964590, -0.41985220, -5.15839640, 5.25022940, 1.05671020, -1.09233420, -1.58522980, 0.60619360, -0.37523350, 2.50086640, -1.39997290, 2.28021660, -2.14687560, -1.48903280, -0.79254376, 3.21804705, -0.94407886, -0.27802316, -0.20753079, -1.12610048, 2.08837350, -0.74248540, 0.44203729, -1.48905938, 1.39644424, -3.89173770, 11.25665848, -9.22884035, 3.26856762, -0.00179541, -2.39664325, 4.00455574, -5.60891295, 4.65563480, -4.40536951, 6.64234497, -7.34787319, 7.56303006, -8.23083674, 4.43247855, 1.31090412 

我想獲得相同的結構如(res$oenb_dependent)中所述。更妙的是,如果我最終data.frame可能看起來像:

. 
. 
2001 Qtr1 -5.06831440 
2001 Qtr2 0.66577130 
2001 Qtr3 3.31613740 
2001 Qtr4 -2.15867040 
2002 Qtr1 -0.78336230 
2002 Qtr2 -0.22032090 
2002 Qtr3 2.41614400 
2002 Qtr4 -1.76254060 
. 
. 
. 

任何建議什麼,是錯誤的,我data.frame反轉。

我怎麼能得出最終的輸出?

我很感謝你的回覆!

回答

2

您可以在一個班輪使用功能從zoo包:

index.ts提取對象的索引ts對象,和yearqtr到季度指數轉換成適當的類:

library(zoo) 
df <- data.frame(yq = yearqtr(index(tt)), val = tt) 

head(df) 
#  yq  val 
# 1 2000 Q4 1.0227039 
# 2 2001 Q1 -5.0683144 
# 3 2001 Q2 0.6657713 
# 4 2001 Q3 3.3161374 
# 5 2001 Q4 -2.1586704 
# 6 2002 Q1 -0.7833623 
1

你可以嘗試以下的(我懷疑有更容易的方式雖然):

library(reshape2) 
the_numbers <- melt(the_ts) 
Year <- c(2000,rep(2001:2014, each=4)) 
Quarter <- c("Qtr4",rep(paste0("Qtr",seq(1:4)),14)) 
df <- cbind(Year,Quarter,the_numbers) 
#> head(df) 
# Year Quarter  value 
#1 2000 Qtr4 1.0227039 
#2 2001 Qtr1 -5.0683144 
#3 2001 Qtr2 0.6657713 
#4 2001 Qtr3 3.3161374 
#5 2001 Qtr4 -2.1586704 
#6 2002 Qtr1 -0.7833623 

數據

the_ts <- structure(c(1.02270390000001, -5.06831440000002, 0.665771299999989, 
        3.31613740000003, -2.15867040000001, -0.783362300000022, -0.22032089999999, 
        2.416144, -1.76254059999999, -0.156503700000002, -7.9803936, 
        9.45947149999999, -4.8104584, 8.48271070000001, -6.18952620000002, 
        1.42885950000002, 1.4896459, -0.419852200000008, -5.1583964, 
        5.25022940000001, 1.0567102, -1.09233420000001, -1.58522979999999, 
        0.606193599999997, -0.375233499999993, 2.50086640000001, -1.39997290000002, 
        2.28021660000002, -2.1468756, -1.4890328, -0.792543760000001, 
        3.21804705000001, -0.944078860000019, -0.278023159999989, -0.207530789999993, 
        -1.12610048000001, 2.0883735, -0.742485400000007, 0.442037290000002, 
        -1.48905938, 1.39644423999999, -3.89173769999999, 11.25665848, 
        -9.22884035, 3.26856762, -0.00179540999998551, -2.39664325000003, 
        4.00455574000001, -5.60891295, 4.6556348, -4.40536951, 6.64234497, 
        -7.34787318999999, 7.56303005999999, -8.23083674, 4.43247855, 
        1.31090412), .Tsp = c(2000.75, 2014.75, 4), class = "ts") 
1

如果tt是您的系列然後fortify.zoo將其轉換:

library(zoo) 
DF <- fortify.zoo(tt) 

捐贈:

> head(DF) 
    Index   tt 
1 2000.75 1.0227039 
2 2001.00 -5.0683144 
3 2001.25 0.6657713 
4 2001.50 3.3161374 
5 2001.75 -2.1586704 
6 2002.00 -0.7833623 

如果你希望你能將指數轉換爲年級:

DF <- transform(DF, Index = as.yearqtr(Index)) 

給出:

> head(DF) 
    Index   tt 
1 2000 Q4 1.0227039 
2 2001 Q1 -5.0683144 
3 2001 Q2 0.6657713 
4 2001 Q3 3.3161374 
5 2001 Q4 -2.1586704 
6 2002 Q1 -0.7833623 
相關問題