Peter Chung Peter Chung - 8 days ago 6
R Question

R wide to long format with three variables

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.

Answer

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
Comments