I have a dataset containing 4 dependent variables, 6 independent variables, and 9 covariates. 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).
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. 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.
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.