-1

I am having trouble using the reshape2 package for the following purpose.

I have a dataframe which looks like:

ID = c("1") 
TIME1 = c("0.5")
TIME2 = c("1") 
TIME3 = c("2")
TIME4 = c("5")
DF = data.frame(ID, TIME1, TIME2, TIME3, TIME4) 

I would like to transform it in order to obtain the following dataframe:

ID = c("1","1","1","1")
TIME = c("0.5", "1", "2","5")
DF2 = data.frame(ID, TIME) 

where ID is repeated in the column.

I apologize a bunch if I have overlooked similar answers on this forum, I just can't seem to wrap my head around the melt and dcast functions and their content.

Furthermore, if any other packages than reshape2 are useful/quicker for this scenario, do not hesitate to post them.

Thank you in advance.

Sincerily,

ykl

ykl
  • 397
  • 2
  • 3
  • 13

1 Answers1

1

You can use melt

library(reshape2)
melt(DF, id.var='ID', value.name='TIME')[,-2]
#  ID TIME
#1  1  0.5
#2  1    1
#3  1    2
#4  1    5

Or because you asked for other packages

library(dplyr)
library(tidyr)
gather(DF, Var, TIME,-ID) %>% 
                          select(-Var)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for the quick reply. What does the [,-2] signify? – ykl Dec 05 '14 at 15:35
  • @yki I am deleting the `2nd` column which is not what you need in the result. You can also read the row and column indexing from `?Extract` – akrun Dec 05 '14 at 15:36