8

I have a dataset in wide form with more than 1500 columns. Since many of the variables are repeated I´d like to reshape into long form. However, r throws an error:

Error in guess(varying) : 
  Failed to guess time-varying variables from their names

Trying to understand this with a toy example I find that

u<-data.frame(id=1:100,f.1=rnorm(100),u.1=rnorm(100),i.1=rnorm(100),f.2=rnorm(100),u.2=rnorm(100),i.2=rnorm(100),
                  f.3=rnorm(100),u.3=rnorm(100),i.3=rnorm(100))

reshape(u,varying=2:10,direction="long")

works fine. However, my data looks more like :

u<-data.frame(id=1:100,f1=rnorm(100),u1=rnorm(100),i1=rnorm(100),f2=rnorm(100),u2=rnorm(100),i2=rnorm(100),
              f3=rnorm(100),u3=rnorm(100),i3=rnorm(100))

reshape(u,varying=2:10,direction="long")

and this is where I´m lost. Any smart idea, except of changing the variable names (which is tiring), of how I can do this?

Misha
  • 3,114
  • 8
  • 39
  • 60
  • Please see my blog [(Reshape Explained I)](http://trinkerrstuff.wordpress.com/2012/05/03/reshape-from-base-explained-part-i/) and [(Reshape Explained II)](http://trinkerrstuff.wordpress.com/2012/05/06/reshape-from-base-explained-part-ii/). They answer a lot about the 'reshape' function. – Tyler Rinker Aug 20 '12 at 23:43
  • That was very helpful..looking forward to part III.. – Misha Aug 22 '12 at 10:32

3 Answers3

15

Add the v.names argument:

reshape(u,varying=2:10,direction="long", v.names=c("f", "u", "i"))
    id time          f          u             i
1.1  1    1  1.7821678  0.5144692  0.0006889928
2.1  2    1 -0.5036801  1.8242030  0.9695553817
3.1  3    1  1.1857706  0.6469423  0.6775602175
4.1  4    1 -0.5759202 -1.0349980  0.7183451146
5.1  5    1 -2.3559773  0.8598020  0.5506339475
6.1  6    1 -0.8047651 -1.4768172 -0.3667918383
...
Andrie
  • 176,377
  • 47
  • 447
  • 496
6

I see Andrie's solution, but perhaps my efforts at understanding rehape syntax can also be useful. The 'varying' argument is supposed to be a named vector (or list) with the column indices grouped by name:

reshape(u, varying=c( f=c(2,5,8), u=c(3,6,9), i=c(4,7,10) ), direction="long")

And this would also have worked (since the names imply a grouping):

 reshape(u,varying=names(u)[2:10], direction="long")

I went back and tried your code and found that it also worked, so I'm wondering if you wanted something different that we are guessing?

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    Could you run the last example as well- I tried it again and it would not run on my machine... – Misha Aug 20 '12 at 22:44
  • That's because there is no separator in those column names. You can get success if you add sep="" to your reshape arguments. – IRTFM Aug 20 '12 at 22:54
  • @Andrie: I used to think the same way. Lately I've been getting the sense that I can actually use it. Learning that you can use the sep argument and that parceling the columns (as above) was a big confidence boost. – IRTFM Aug 21 '12 at 21:17
5

Just add option sep = "" to let reshape knows that your columns name is not separate by ..

Verbal
  • 147
  • 1
  • 8