我有一个数据框,想要将它从宽格式更改为长格式但我有三个变量,而ref = RR_ref,het = RR_het和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
任何人都可以帮助我吗?感谢。
答案 0 :(得分:1)
我们可以在更改列的顺序后使用melt
中的data.table
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