1

If I have data from:

df <- data.frame(x=sample(letters[1:20], replace=T),
 y=sample(letters[1:20], replace=T), z=sample(1:10, replace=T))

and I want to stack the x and y 'id' columns, and thus repeat the value, how would I do that?

So, pretend the first row of the data frame is

   x y  z
1  p r  5

I want the first row in a reshaped data frame to look like:

   id value
1  p  5
2  r  5
knl
  • 377
  • 2
  • 3
  • 9
  • http://stackoverflow.com/questions/1181060/reshaping-time-series-data-from-wide-to-tall-format-for-plotting – SabDeM May 24 '15 at 00:31

1 Answers1

1
library(data.table)
melt(setDT(df),measure.vars = 1:2)
user227710
  • 3,164
  • 18
  • 35
  • 1
    ah ok, should have just played with melt more. Weird to me to set the 'result' column as the id. – knl May 24 '15 at 00:35