My sample dataframe in R looks the following:

```
     NAME  ID SURVEY_YEAR REFERENCE_YEAR CUMULATIVE_SUM CUMULATIVE_SUM_REFYEAR
1 NAME1  47        1960           1959             -6                      0
2 NAME1  47        1961           1960            -10                     -6
3 NAME1  47        1963           1961             NA                     NA
4 NAME1  47        1965           1963            -23                    -10
5 NAME2 259        2007           2004             -9                      0
6 NAME2 259        2009           2007             NA                     NA
7 NAME2 259        2010           2009             NA                     NA
8 NAME2 259        2011           2010             NA                     NA
9 NAME2 259        2014           2011            -40                     -9

```

What I am trying to do is to rearrange the dataframe in the following way.

```
NAME    ID  time            YEAR    CUMULATIVE_SUM  id
NAME1   47  REFERENCE_YEAR  1959    0               1
NAME1   47  SURVEY_YEAR     1960    -6              1
NAME1   47  REFERENCE_YEAR  1960    -6              2
NAME1   47  SURVEY_YEAR     1961    -10             2
NAME1   47  REFERENCE_YEAR  1961    NA              3
NAME1   47  SURVEY_YEAR     1963    NA              3
NAME1   47  REFERENCE_YEAR  1963    -10             4
NAME1   47  SURVEY_YEAR     1965    -23             4
NAME2   259 REFERENCE_YEAR  2004    0               5
NAME2   259 SURVEY_YEAR     2007    -9              5
NAME2   259 REFERENCE_YEAR  2007    NA              6
NAME2   259 SURVEY_YEAR     2009    NA              6
NAME2   259 REFERENCE_YEAR  2009    NA              7
NAME2   259 SURVEY_YEAR     2010    NA              7
NAME2   259 REFERENCE_YEAR  2010    NA              8
NAME2   259 SURVEY_YEAR     2011    NA              8
NAME2   259 REFERENCE_YEAR  2011    -9              9
NAME2   259 SURVEY_YEAR     2014    -40             9    

```

I am trying to do this the following way but this is not exactly what I want.

```
# read in data
data <- data.frame(NAME=c("NAME1", "NAME1","NAME1","NAME1","NAME2","NAME2","NAME2","NAME2","NAME2"),
                   ID=c(47,47,47,47,259,259,259,259,259),
                   SURVEY_YEAR=c(1960,1961,1963,1965,2007,2009,2010,2011,2014), 
                   REFERENCE_YEAR=c(1959,1960,1961,1963,2004,2007,2009,2010,2011),
                   CUMULATIVE_SUM=c(-6,-10,NA,-23,-9,NA,NA,NA,-40),
                   CUMULATIVE_SUM_REFYEAR=c(0,-6,NA,-10,0,NA,NA,NA,-9))  

# reshape data  
dat3 <- reshape(data, direction="long",
                    varying = list(c("SURVEY_YEAR","REFERENCE_YEAR"), c("CUMULATIVE_SUM","CUMULATIVE_SUM_REFYEAR")), 
                    v.names=c("YEAR","CUMULATIVE_SUM"), times = c("SURVEY_YEAR", "REFERENCE_YEAR"))

```

Does anyone have an idea how to reshape the dataframe correctly so that it corresponds to the desired solution above? Thanks.