2014-02-05 83 views
1

我有來自IMF IFS的季度時間序列經濟數據,我需要進入長期形式。R在整形時間序列數據

現在,行是每個國家的變量,列是時間,所以它看起來像這樣。

 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變量和測量變量都在行的任何建議。

回答

1

它是一個局部melt後跟一個dcastreshape2包:

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)] 

給出:使用stackreshape

> 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 
0

基地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