在R上创建面板数据集

时间:2019-06-19 05:16:26

标签: r

我已使用以下代码将我的数据集转换为面板数据集:

我一直在使用splitstackshape

library(splitstackshape)

set.seed(123)
dt1 <- data.frame(id = LETTERS[1:10],
              var1_2012 = runif(10, min = 1, max = 10),
              var2_2012 = runif(10, min = 1, max = 10),
              var1_2013 = runif(10, min = 1, max = 10),
              var2_2013 = runif(10, min = 1, max = 10))



dt2 <- splitstackshape::merged.stack(dt1,
                                 id.vars = "id",
                                 var.stubs = c("var1", "var2"),
                                 sep = "_",
                                 keep.all = TRUE)

但是我一直收到此错误:

if(ncol(x)== 1L){时,参数长度为零

1 个答案:

答案 0 :(得分:0)

splitstackshape版本1.4.8上可以正常使用

splitstackshape::merged.stack(dt1,
                                  id.vars = "id",
                                  var.stubs = c("var1", "var2"),
                                  sep = "_",
                                  keep.all = TRUE)
#    id .time_1     var1     var2
# 1:  A    2012 3.588198 9.611500
# 2:  A    2013 9.005854 9.667218
# 3:  B    2012 8.094746 5.080007
# 4:  B    2013 7.235231 9.120691
# 5:  C    2012 4.680792 7.098136
# 6:  C    2013 6.764561 7.216348
# 7:  D    2012 8.947157 6.153701
# 8:  D    2013 9.948428 8.159207
# 9:  E    2012 9.464206 1.926322
#10:  E    2013 6.901352 1.221523
#11:  F    2012 1.410008 9.098425
#12:  F    2013 7.376774 5.300164
#13:  G    2012 5.752949 3.214790
#14:  G    2013 5.896594 7.826136
#15:  H    2012 9.031771 1.378536
#16:  H    2013 6.347278 2.947671
#17:  I    2012 5.962915 3.951286
#18:  I    2013 3.602438 3.863629
#19:  J    2012 5.109533 9.590533
#20:  J    2013 2.324023 3.084632