1

this is my first question here :) I want to reshape my data using R, I create some fake data. but real data would be a large unbalanced panel. If there is anything I should care about especially in unbalanced panel, I am looking forward to learning. Thanks!

here is the wide one.

old <- read.table(header = T, text='
              ID s1.2005 s1.2006 s1.2007 s2.2005 s2.2006 s3.2007
              1   342 333 233 374 734 384
              2   488 495 485 334 245 NA
              3   485 987 NA 244 211 NA')

I try to reshape to long one as below.

long <- read.table(header =T, text= '
               ID year s1 s2
               1 2005 342 374 
               1 2006 333 734
               1 2007 233 384
               2 2005 488 334
               2 2006 495 245
               2 2007 485 NA
               ')

well, I tried reshape function, but seems like not working. here is my code.

long <- reshape(old, 
            direction = "long",
            varying = list(names(old)[2:6]),
            v.names = c("s1","s2"),
            idvar = "ID",
            timevar = "year",
            times = 2005:2007)
EvanWang
  • 11
  • 1

0 Answers0