0

I have the following

rows names for df (AA, AB, AC)

df$12(WT, WT, MUT)

df$34(WT, WT, MUT)

df$56(MUT, WT, WT)

     12   34  56
AA   WT   WT  MUT
AB   WT   WT  WT
AC   MUT  MUT WT

I would like to get the following df2

df2$GS(AA, AB, AC, AA, AB, AC, AA, AB, AC)

df2$SI(12, 12, 12, 34, 34, 34, 56, 56, 56)

df2$NT(WT, WT, MUT, WT, WT, MUT, MUT, WT, WT)

     GS   SI  NT
     AA   12  WT
     AB   12  WT
     AC   12  MUT
     AA   34  WT
     AB   34  WT
     AC   34  MUT
     AA   56  MUT
     AB   56  WT
     AC   56  WT

My current code so far is

setDT(df, keep.rownames = TRUE)[]

But after that I am a bit lost on the next set of code to generate df2. Any help would be greatly appreciated.

vcat
  • 263
  • 1
  • 9
  • Are you using a `data.table` instead of a `data.frame`? Do you need a `data.table` solution? – Martin Gal Jun 15 '21 at 14:56
  • 1
    Assuming you want to use data.table try this: `library(data.table); DT <- as.data.table(df, keep.rownames = "GS"); M <- melt(DT, id = "GS", value.name = "NT", variable.name = "SI"); M[, SI:=as.integer(as.character(SI))]` – G. Grothendieck Jun 15 '21 at 15:22
  • Or with base R: `df3 <- setNames(as.data.frame.table(as.matrix(df)), c("GS", "SI", "NT")); df3$SI <- as.integer(as.character(df3$SI))` – G. Grothendieck Jun 15 '21 at 15:31
  • data.frame is preferred, but data. table would work. Your suggestions worked. Thank you! – vcat Jun 15 '21 at 15:51

0 Answers0