Search code examples
rreshape

Error in reshape(): duplicate 'row names' are not allowed


I have wide longitudinal data that I would like to reshape into long data. This is a sample:

sex group id sex.1 group.1    status1  beg1  end1 status2  beg2  end2
1 1000   1     a 1000     1       a Vocational  <NA> S2007      HE S2007 S2008
2 1001   1     a 1001     1       a Vocational  <NA> S2007      HE S2008 S2012
3 1004   1     a 1004     1       a Vocational  <NA> S2008     999  <NA>  <NA>
4 1006   2     a 1006     2       a Vocational  <NA> S2007    Army S2012  <NA>
5 1007   1     a 1007     1       a         HE  <NA> S2007     999  <NA>  <NA>
6 1008   1     a 1008     1       a Vocational S2013  <NA>     999  <NA>  <NA>

I need to get it in this shape, compatible with SPELL format:

  id sex  group index  status    beg     end
1000  1    a      1   Vocational  NA     S2007
1000  1    a      2      HE      S2008   S2012
...

I am using the following command:

spell <- reshape(data, 
                 varying=names(data)[4:60],
                 direction="long",
                 idvar=c("id","sex","group"),
                 sep="")   

And I get the following error message:

    Error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1L],  : 
duplicate 'row.names' are not allowed
        In addition: Warning message: non-unique value when setting 'row.names': ‘NA.1’ 

I have tried setting NA values to 999 this way, but it does not work.

data[is.na(data)] <- 999

Do you know what may get this to work? thanks a lot beforehand!


Solution

  • x2 <- reshape(mydata, idvar=c("id.1", "sex.1", "group.1"), direction="long", 
                  varying=list(c(7, 10), c(8, 11), c(9, 12)), 
                  v.names=c("status","beg","end"))
    
    head(x2)
    
                 id sex group id.1 sex.1 group.1 time     status   beg   end
    1000.1.a.1 1000   1     a 1000     1       a    1 Vocational  <NA> S2007
    1001.1.a.1 1001   1     a 1001     1       a    1 Vocational  <NA> S2007
    1004.1.a.1 1004   1     a 1004     1       a    1 Vocational  <NA> S2008
    1006.2.a.1 1006   2     a 1006     2       a    1 Vocational  <NA> S2007
    1007.1.a.1 1007   1     a 1007     1       a    1         HE  <NA> S2007
    1008.1.a.1 1008   1     a 1008     1       a    1 Vocational S2013  <NA>