将季度时间序列(类ts)转换为data.frame

时间:2015-07-20 19:24:19

标签: r statistics

我正在尝试将季度时间序列转换为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反转是错误的。

我如何得出最终输出?

感谢您的回复!

3 个答案:

答案 0 :(得分: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 :(得分: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")

答案 2 :(得分: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

如果您希望将索引转换为yearqtr类:

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