您可以从iemelt
的开发版本中尝试。它可以将多个变量作为一个列表。安装开发版本的说明是data.table
v1.9.5
measure.vars
here
library(data.table)#v1.9.5+
melt(setDT(df1), measure.vars=list(c(3,5), c(4,6)),
value.name=c('V1', 'V2'))[,variable:=NULL][order(ID)]
# ID Gender V1 V2
#1: 1 M 1 2
#2: 1 M 3 4
#3: 2 F 5 6
#4: 2 F 7 8
或使用reshape
来自base R
res <- subset(reshape(df1, idvar=c('ID', 'Gender'),
varying=list(c(3,5), c(4,6)), direction='long'), select=-time)
row.names(res) <- NULL
更新
如果我们需要将“df2”转换回“df1”,可以使用dcast
from 。data.table
它可以包含多个value.var
列。在继续之前,我们需要N
按组('ID','Gender')创建一个序列列()dcast
dcast(setDT(df2)[, N:=1:.N, list(ID, Gender)], ID+Gender~N,
value.var=c('V1', 'V2'))
# ID Gender 1_V1 2_V1 1_V2 2_V2
#1: 1 M 1 3 2 4
#2: 2 F 5 7 6 8
或者我们按组创建一个序列,ave
然后使用reshape
from base R
。
df2 <- transform(df2, N= ave(seq_along(ID), ID, Gender, FUN=seq_along))
reshape(df2, idvar=c('ID', 'Gender'), timevar='N', direction='wide')
# ID Gender V1.1 V2.1 V1.2 V2.2
#1 1 M 1 2 3 4
#3 2 F 5 6 7 8
数据
df1 <- data.frame(ID = c(1, 2), Gender = c("M","F"), Q1 = c(1, 5),
Q2 = c(2, 6), Q3 = c(3, 7), Q4 = c(4, 8))
df2 <- data.frame(ID = c(1, 1, 2, 2), Gender = c("M", "M", "F", "F"),
V1 = c(1, 3, 5, 7), V2 = c(2, 4, 6, 8))