0

I have a table as follows.

 A   B   C
14.29   20.06   20.04
19.10   20.64   26.23
19.09   18.00   22.74
16.25   19.56   22.04
15.09   19.47   23.37
16.61   19.07   25.02
19.63   18.38   23.27

But I want to rotate the table so it would be like.

group    time
A    15.5
A    16.5
A    12.3
A    15.6
B    14.2
B    13.5
B    11.2
C    11.5
C    11.2
C    11.8

I referred to the reshape() function but I have a feeling it might be the wrong tool for the job because I was getting this error:

> x.long <- reshape(x, varying = 2:3, direction = "long")
Error in guess(varying) : 
  failed to guess time-varying variables from their names
> help(reshape)
> x.long <- reshape(x, varying = 1:3, direction = "long")
Error in guess(varying) : 
  failed to guess time-varying variables from their names
> x.long <- reshape(x, varying = 0:2, direction = "long")
Error in guess(varying) : 
  failed to guess time-varying variables from their names
> x.long <- reshape(x, varying = 1, direction = "long")
Error in guess(varying) : 
  failed to guess time-varying variables from their names
> x.long <- reshape(x, varying = 1:7, direction = "long")
Error in guess(varying) : 
  failed to guess time-varying variables from their names
> x.long <- reshape(x, direction = "long")
Error in reshape(x, direction = "long") : 
  no 'reshapeWide' attribute, must specify 'varying'
> x.long <- reshape(x, direction = "wide")
Error in `[.data.frame`(data, , timevar) : undefined columns selected
> 
prab4th
  • 231
  • 1
  • 2
  • 11
  • reshaping didn't work at the first try, and it was too complicated as I believe for this simple task. `stack()` worked fine for this matter. – prab4th Oct 09 '16 at 05:59

1 Answers1

0

We can use stack from base R

setNames(stack(df1)[2:1], c("group", "time"))

Or use melt from reshape2

library(reshape2)
melt(df1, variable.name = "group", value.name = "time")
akrun
  • 874,273
  • 37
  • 540
  • 662