R从宽到长的格式,具有三个变量
R wide to long format with three variables
我有一个数据框,想将它从宽格式更改为长格式,但我有三个变量,而 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
谁能帮帮我?谢谢
我们可以在更改列的顺序后使用 data.table
中的 melt
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
我有一个数据框,想将它从宽格式更改为长格式,但我有三个变量,而 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
谁能帮帮我?谢谢
我们可以在更改列的顺序后使用 data.table
中的 melt
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