宽到长,带有两组变量以重塑

时间:2018-09-07 16:23:07

标签: r reshape

我有一个数据集,其中包含4个因变量,6个因变量和9个协变量。数据集以宽格式排列。

我希望采用长格式,因此我可以循环执行某个过程(lm,plot,...),更改因变量和自变量,同时保留协变量(在lm示例中)。

我的数据如下:

enter image description here

我想要这样:

enter image description here

如果我只有一组变量(仅依赖或仅依赖),我知道该怎么做,我会使用 reshape 函数。但是,当我有两组变量时该怎么办?我需要创建DV和IV的24种不同组合,同时在每一行中保留所有协变量。

希望您能提供一些指导。预先谢谢您!

用于以我的数据形式生成随机数据的代码:

ID = seq(1,5)
DV1 = rnorm(5,0,1)
DV2 = rnorm(5,0,1)
DV3 = rnorm(5,0,1)
DV4 = rnorm(5,0,1)
IV1 = rnorm(5,0,1)
IV2 = rnorm(5,0,1)
IV3 = rnorm(5,0,1)
IV4 = rnorm(5,0,1)
IV5 = rnorm(5,0,1)
IV6 = rnorm(5,0,1)
COV1 = rnorm(5,0,1)
COV2 = rnorm(5,0,1)
COV3 = rnorm(5,0,1)
data = data.frame(ID,DV1,DV2,DV3,DV4,IV1,IV2,IV3,IV4,IV5,IV6,COV1,COV2,COV3)

1 个答案:

答案 0 :(得分:0)

您可以将melt包的reshape2函数与参数id一起使用,在其中您指示在从宽格式到窄格式转换期间要保留的列。请参见下面的代码:

set.seed(123)
ID = seq(1,5)
DV1 = rnorm(5,0,1)
DV2 = rnorm(5,0,1)
DV3 = rnorm(5,0,1)
DV4 = rnorm(5,0,1)
IV1 = rnorm(5,0,1)
IV2 = rnorm(5,0,1)
IV3 = rnorm(5,0,1)
IV4 = rnorm(5,0,1)
IV5 = rnorm(5,0,1)
IV6 = rnorm(5,0,1)
COV1 = rnorm(5,0,1)
COV2 = rnorm(5,0,1)
COV3 = rnorm(5,0,1)
data = data.frame(ID,DV1,DV2,DV3,DV4,IV1,IV2,IV3,IV4,IV5,IV6,COV1,COV2,COV3)

library(reshape2)
data_molten <- melt(data, id = c("ID", "DV1", "DV2", "DV3", "DV4", "COV1", "COV2", "COV3"))
head(data_molten)

输出:

  ID         DV1        DV2        DV3        DV4        COV1       COV2       COV3 variable      value
1  1 -0.56047565  1.7150650  1.2240818  1.7869131  0.25331851  1.5164706  0.3796395      IV1 -1.0678237
2  2 -0.23017749  0.4609162  0.3598138  0.4978505 -0.02854676 -1.5487528 -0.5023235      IV1 -0.2179749
3  3  1.55870831 -1.2650612  0.4007715 -1.9666172 -0.04287046  0.5846137 -0.3332074      IV1 -1.0260044
4  4  0.07050839 -0.6868529  0.1106827  0.7013559  1.36860228  0.1238542 -1.0185754      IV1 -0.7288912
5  5  0.12928774 -0.4456620 -0.5558411 -0.4727914 -0.22577099  0.2159416 -1.0717912      IV1 -0.6250393
6  1 -0.56047565  1.7150650  1.2240818  1.7869131  0.25331851  1.5164706  0.3796395      IV2 -1.6866933