I'm new to reshaping data and would like to take a wide dataset without a time variable and make it long. The variable names capture time. For example, I would like to take this frame:
frame <- read.table(header = TRUE, text = "id month1 month2 month3
1 1 0 0 1
2 2 0 1 0
3 3 0 0 0
4 4 0 0 1
5 5 0 1 1
6 6 0 0 0")
frame
id month1 month2 month3
1 1 0 0 1
2 2 0 1 0
3 3 0 0 0
4 4 0 0 1
5 5 0 1 1
6 6 0 0 0
and make it into:
id month1 time
1 1 0 1
1 1 0 2
1 1 1 3
2 2 0 1
2 2 1 2
2 2 0 3
...
6 6 0 1
6 6 0 2
6 6 0 3
I tried the following code, but it doesn't work:
reshape(data = frame, direction = "long", idvar = "id", timevar = "time", varying = c(2:4))
Any and all thoughts welcome. Thanks.