简体   繁体   English

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

[英]Wide to Long with Two sets of Variables to Reshape

I have a dataset containing 4 dependent variables, 6 independent variables, and 9 covariates. 我有一个数据集,其中包含4个因变量,6个独立变量和9个协变量。 The dataset is arranged in wide format. 数据集以宽格式排列。

I wish to make it in long format, so I can repeat a certain procedure (lm, plot, ...) in a loop, varying the dependent variable and the independent, while keeping the covariates (in lm example). 我希望采用长格式,因此我可以循环执行某个过程(lm,plot,...),改变因变量和独立变量,同时保留协变量(在lm示例中)。

The data I have looks like: 我的数据如下所示:

在此处输入图片说明

And I want it like: 我想要这样:

在此处输入图片说明

If I had only one set of variables (dependent only or independent only), I know how to do it, I would use the reshape function. 如果我只有一组变量(仅依赖或仅依赖),我知道该怎么做,那么我将使用reshape函数。 But how do I do it when I have two sets of variables ? 但是,当我有两组变量时该怎么办? I need to create 24 different combinations of DV and IV while keeping all covariates in every row. 我需要创建DV和IV的24种不同组合,同时在每一行中保留所有协变量。

Would appreciate some guidance. 希望能得到一些指导。 Thank you in advance ! 先感谢您 !

Code for generating random data in the shape of my data: 用于生成数据形状的随机数据的代码:

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)

You can use melt function of reshape2 package with argument id in which you indicate the columns you would like to keep during transformation from wide to narrow format. 您可以使用带有参数idreshape2包的melt函数,在其中指定从宽格式到窄格式转换期间要保留的列。 Please see the code below: 请参见下面的代码:

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)

Output: 输出:

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM