1

My data.frame

a<-sample(12)
b<-sample(-100:100, 12)
d<-c(-11:0)
O<-rep(c("N","H"), each=6)
H<-rep(c("In+", "In-"), each=3, times=2) 
ID<-rep(c("bo","co", "do", "fo"), each=3)
mydata_1<-data.frame(ID, a, b, d, O, H)

I want to melt the dataframe variables a, b, d; while O and H should be ordered like the ID. My solution below:

mydata_2<-data.frame(ID, a, b, d) 
gg.df <- melt(mydata, id="ID", variable.name="int")
O<-rep(c("N","H"), each=6, times=3)
H<-rep(rep(c("In+", "In-"), each=3, times=2), times=3)  
gg.df[, "OX"] <- O
gg.df["HI"] <- H

I am wondering how this can be done inside the melt function by using the full dataframe (mydata_1)

Al14
  • 1,734
  • 6
  • 32
  • 55
  • 3
    Have you tried `melt(mydata_1, id.var=c("ID", "O", "H"))` – akrun Sep 13 '15 at 14:29
  • It seems easier with tidyr:gather `library(tidyr); gather(mydata_1, "int", "value", 2:4)` and as the original dataset and have the order you want, keep it. – PereG Sep 13 '15 at 14:53

0 Answers0