【问题标题】:reshaping time series data in R在 R 中重塑时间序列数据
【发布时间】:2014-03-02 17:04:04
【问题描述】:

我有来自国际货币基金组织 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 变量和测量变量都在行中时,我无法找到有关使用 reshape 的任何建议。

【问题讨论】:

    标签: r panel-data


    【解决方案1】:

    使用stack和reshape的基本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
    

    【讨论】:

      【解决方案2】:

      它是reshape2 包中的部分melt 后跟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)]
      

      给予:

      > 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多