在R中重塑时间序列数据

时间:2014-02-05 23:58:39

标签: r panel-data

我有来自国际货币基金组织IFS的季度时间序列经济数据,我需要进入长期形式。

现在,行是每个国家/地区的变量,列是时间,所以它看起来像这样。

     country  variable    Q1  Q2 
[1,] "USA"   "inflation" "1" "5"
[2,] "USA"   "GDPPC"     "2" "6"
[3,] "UK"    "inflation" "3" "7"
[4,] "UK"    "GDPPC"     "4" "8"

我需要把它变成长形式:

    country  Time  inflation    GDPPC
[1,] "USA"   "Q1" "1"          "2"  
[2,] "USA"   "Q2" "5"          "6"  
[3,] "UK"    "Q1" "3"          "4"  
[4,] "UK"    "Q2" "7"          "8" 

当ID变量和测量变量都在行中时,我无法找到有关使用重塑的任何建议。

2 个答案:

答案 0 :(得分:1)

部分melt后跟dcast包中的reshape2

d = data.table(country = c("USA","USA","UK","UK"), variable = c("inflation","GDPPC","inflation","GDPPC"),Q1=as.character(1:4),Q2=as.character(5:8))
require(reshape2)
d2 = melt(d, id=c("country", "variable"))
colnames(d2)[3] = "Time" 
rr=dcast(d2, country +Time ~ variable)
rr = rr[order(rr$country,decreasing=T),c(1:2,4,3)]

给出:

> rr
  country Time inflation GDPPC
3     USA   Q1         1     2
4     USA   Q2         5     6
1      UK   Q1         3     4
2      UK   Q2         7     8

答案 1 :(得分:0)

使用stackreshape的基本R方法,使用下面的data.frame

d <- data.frame(country = c("USA","USA","UK","UK"), variable = c("inflation","GDPPC","inflation","GDPPC"),Q1=1:4,Q2=5:8)

重塑:

intm <- data.frame(d[,c("country","variable")],stack(d[,c("Q1","Q2")]))
reshape(intm, idvar=c("country","ind"), timevar="variable", direction="wide")

#  country ind values.inflation values.GDPPC
#1     USA  Q1                1            2
#3      UK  Q1                3            4
#5     USA  Q2                5            6
#7      UK  Q2                7            8