I want to reshape the below dataset into a long format. I tried the melt functino in reshape, but I do not know how to change the age1,age2,age3 into one column. Can anyone help? Thank you.
id <- c(1,2,3,4,5)
age1 <- c(11,11,11,11,11)
age2 <- age1+2
age3 <- age2+2
ht1 <- c(120,130,125,121,130)
ht2 <- ht1 + 20
ht3 <- ht2 + 20
bmi1 <- c(18,19,17,18,18)
bmi2 <- c(20,18,19,21,24)
bmi3 <- c(21,21,21,24,27)
df <- data.frame(id=id,age1=age1,age2=age2,age3=age3,ht1=ht1,ht2=ht2,ht3=ht3,bmi1=bmi1,bmi2=bmi2,bmi3=bmi3)
from
id sex age1 age2 age3 ht1 ht2 ht3 bmi1 bmi2 bmi3
1 1 M 11 13 15 120 140 160 18 20 21
2 2 F 11 13 15 130 150 170 19 18 21
3 3 M 11 13 15 125 145 165 17 19 21
4 4 F 11 13 15 121 141 161 18 21 24
5 5 M 11 13 15 130 150 170 18 24 27
to something like this
id sex age ht bmi
1 M 11 120 18
1 M 13 140 20
1 M 15 160 21
2 F 11 130 19
2 F 13 150 18
2 F 15 170 21
3 M 11 165 17
...