在R中重塑时间序列数据

时间:2022-06-06 23:10:50

I have quarterly time series economic data from the IMF IFS that I need to get into long form.

我有来自IMF IFS的季度时间序列经济数据,我需要进入长期形式。

Right now, the rows are variables per country and the columns are time, so it looks something like this.

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

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

I need to get it into long form:

我需要把它变成长形式:

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

I haven't been able to find any advice on using reshape when the ID variable and the measurement variables are both in rows.

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

2 个解决方案

#1


1  

It is a partial melt followed by a dcast in the reshape2 package:

这是一个部分融化,然后是reshape2包中的dcast:

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

Gives:

得到:

> 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

#2


0  

Base R method using stack and reshape, using the below data.frame

使用stack和reshape的Base R方法,使用下面的data.frame

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

Reshape away:

重塑:

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

#1


1  

It is a partial melt followed by a dcast in the reshape2 package:

这是一个部分融化,然后是reshape2包中的dcast:

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

Gives:

得到:

> 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

#2


0  

Base R method using stack and reshape, using the below data.frame

使用stack和reshape的Base R方法,使用下面的data.frame

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

Reshape away:

重塑:

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