0

I have a data frame and want to change it from wide to long format but I have three variables, whereas ref = RR_ref, het = RR_het and hom = RR_hom.

df:
Number  ref het RR_het  hom RR_hom  RR_ref
mary    GG  AG  0.29    AA  0.0841  1
wayne   AA  AG  1.7     GG  2.89    1

structure(list(Number = c("mary", "wayne"), ref = c("GG", "AA"
), het = c("AG", "AG"), RR_het = c(0.29, 1.7), hom = c("AA", 
"GG"), RR_hom = c(0.0841, 2.89), RR_ref = c(1L, 1L)), .Names = c("Number", 
"ref", "het", "RR_het", "hom", "RR_hom", "RR_ref"), class = "data.frame", row.names = c(NA, -2L))

desired output:
Name    Vars    Value
mary    GG      1
wayne   AA      1
mary    AG      0.29
wayne   AG      1.7
mary    AA      0.0841
wayne   GG      2.89

Anyone can help me? Thanks.

Peter Chung
  • 1,010
  • 1
  • 13
  • 31

1 Answers1

1

We can use melt from data.table after changing the order of columns

library(data.table)
library(gtools)
melt(setDT(df[c(names(df)[1], mixedsort(names(df)[-1]))]),
    measure = patterns("^[a-z]+$", "_"), 
    value.name = c("Vars", "Value"))[, variable := NULL][]
#   Number Vars  Value
#1:   mary   AG 0.2900
#2:  wayne   AG 1.7000
#3:   mary   AA 0.0841
#4:  wayne   GG 2.8900
#5:   mary   GG 1.0000
#6:  wayne   AA 1.0000
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thanks but the value is not correct in mary Vars GG is equal to 1.0000, AG is 0.29 and AA is 0.0841 – Peter Chung Nov 24 '16 at 06:15
  • @PeterChung Can you provide a little more description as to how to want to make it long. Is it based on the character and numeric columns? – akrun Nov 24 '16 at 06:19
  • I want make it long for matching another data frame which is full of Vars like GG AA and AG, so that I can match them into the value number for calculation. so i think it is based on the character columns – Peter Chung Nov 24 '16 at 06:22
  • @PeterChung Your initial dataset have a random order of columns. I changed the order with `mixedsort`. Can you check it now? – akrun Nov 24 '16 at 06:39
  • thanks a lot, it works now – Peter Chung Nov 24 '16 at 06:52