Reformat data to r when reshape cannot guess time variable names

I have a wide form dataset with over 1500 columns. Since many of the variables are repeated, I would like to reformat it to a long form. However, r causes an error:

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

Trying to figure this out with a toy example, I found 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 great. However, my data is 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 here I lost. Any clever idea, except changing variable names (which is tedious), how can I do this?

+5
source share
3 answers

Add an argument v.names:

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
...
+8
source

Andrie, , , rehape . "" ( ) , :

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

( ):

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

, , , - , ?

+5

sep = "", reshape , ..

+1

All Articles