0

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.

rawr
  • 20,481
  • 4
  • 44
  • 78
coding_heart
  • 1,245
  • 3
  • 25
  • 46
  • 3
    you just need `varying = list(2:4)` instead of `varying = c(2:4)` methinks – rawr Oct 26 '15 at 23:05
  • also you should set a seed since you are randomly sampling. we can't get the same data you have without that – rawr Oct 26 '15 at 23:06
  • 1
    I'm closing this as a dupe. If you think, it's not- you are welcome to ping me. – David Arenburg Oct 26 '15 at 23:07
  • Thanks for pointing out the other post. – coding_heart Oct 26 '15 at 23:21
  • 2
    @rawr - `2:4` also works if you specify `sep=""` - `reshape(frame, idvar="id", direction="long", varying=2:4, sep="")` , with the added bonus that it will also correctly pick the output variable name as `"month"` – thelatemail Oct 27 '15 at 01:49
  • 1
    @thelatemail I will never understand reshape – rawr Oct 27 '15 at 03:14
  • @rawr - in this case it is because say you had other variables in columns `5:7` called `blah1/blah2/blah3` you could just do `varying=2:7, sep=""` and `reshape` would guess that you have two sets of variables, one for `month`s and one for `blah`s. If you formally state which variables are in each set using `varying=list(2:4,5:7)` you don't have to give `reshape` a hint as to how the different sets are formatted so they can be automatically dealt with. [The more you know.](http://i.imgur.com/g8jPjje.jpg) – thelatemail Oct 27 '15 at 03:28

0 Answers0