I need to convert a "wide" dataframe of annually repeated measures on individuals into "long" format so that I can model it like lm(y_year2 ~ x_year1) as well as lm(z_year2 ~ y_year2)
I can get it into the format I want "by hand" but cannot get figure out how to melt
/dcast
it into the shape I want
Below I've illustrated what I'm doing with some simulated data
The dataframe is like this in wide format, one individual per line
ID SITE L_03 M_03 R_03 L_04 M_04 R_04 L_05 M_05 R_05
1 forest X a YES Y b YES Z c NO
2 forest ...
I'd like it in LONG format:
ID SITE L_year1 L_year2 M_year1 M_year2 R_year1 R_year2 year1 year2
1 forest Z Y a b YES YES 03 04
1 forest Y Z b c YES NO 04 05
2 forest ...
2 forest ...
Some Simulated data: L and M are numeric (length & mass), R is a Yes/No factor (reproductive), 3 years of repeated measurements (2003-2005)
ID <- 1:10; SITE <- c(rep("forest",3), rep("swamp",3), rep("field",4))
L_03 <- round(rnorm(10, 100, 1),3) ; M_03 <- round((10 + L_03*0.25 + rnorm(10, 0, 1)), 3)
R_03 <- sample(c("Yes", "No"), 10, replace = TRUE) ; L_04 <- round((2 + L_03*1.25 + rnorm(10, 1,10)), 3)
M_04 <- round((10 + L_04*0.25 + rnorm(10, 0,10)), 3) ;R_04 <- sample(c("Yes", "No"), 10, replace = TRUE)
L_05 <- round((2 + L_04*1.25 + rnorm(10, 1,10)),3) ; M_05 <- round((10 + L_05*0.25 + abs(rnorm(10, 0,10))),3)
R_05 <- sample(c("Yes", "No"), 10, replace = TRUE); rm_data <- data.frame(ID, SITE, L_03, M_03, R_03, L_04, M_04,R_04, L_05, M_05, R_05)
Approach 1: My ad hoc reshaping "by hand" with rbind
1st, make subset with 2003 & 2004 data, then another w/ 2004 & 2005
rm_data1 <- cbind(rm_data[ ,c(1,2,3:5, 6:8)], rep(2003,10), rep(2004,10))
rm_data2 <- cbind(rm_data[ ,c(1,2,6:8, 9:11)],rep(2004,10), rep(2005,10))
names(rm_data1)[3:10]<- c("L1", "M1", "R1", "L2", "M2", "R2", "yr1", "yr2")
names(rm_data2)[3:10]<- c("L1", "M1", "R1", "L2", "M2", "R2", "yr1", "yr2")
data3 <- rbind(rm_data1, rm_data2)
Approach 2?: I'd like to do this with reshape
/melt
/dcast
. I can't figure out if I can use dcast
directly on the wide dataframe or, once I melt
it, how to dcast
it into the format I want.
library(reshape2)
rm_measure_vars <- c("L_03", "M_03", "R_03", "L_04", "M_04","R_04", "L_05", "M_05", "R_05")
rm_data_melt <- melt(data = rm_data, id.vars = c("ID", "SITE"), measure.vars = rm_measure_vars, value.name = "data")
I add a designator of the year the measurement was taken to the melted data
obs_year <- gsub("(.*)([0-9]{2})", "\\2", rm_data_melt$variable)
rm_data_melt <- cbind(rm_data_melt, obs_year)
The dcast
seems like it should be something like this, but this is not yet what I need
dcast(data = rm_data_melt, formula = ID + SITE + obs_year ~ variable)
ID SITE obs_year L_03 M_03 R_03 L_04 M_04 R_04 L_05 M_05 R_05
1 1 forest 03 99.96 35.364 No <NA> <NA> <NA> <NA> <NA> <NA>
2 1 forest 04 <NA> <NA> <NA> 129.595 47.256 Yes <NA> <NA> <NA>
3 1 forest 05 <NA> <NA> <NA> <NA> <NA> <NA> 177.607 58.204 Yes
Any suggestions would be greatly appreciated