Here is a sample set with only 2 questions. You can modify this to extend to 85.
In this case, I am using the melt( )
function in the data.table
package to convert the data from wide format to long format. The id.vars
are variables you would like to keep as columns and the measure.vars
are variables you would like condensed into a new column Question
with corresponding values in Response
.
library(data.table)
lmh14 <- data.table(
ID = c(1,2,3,4),
education_code = c(20,50,20,60),
age = c(87,67,56,52),
sex = c("F","M","M","M"),
question1 = c(NA_real_,1,5,3),
question2 = c(4,3,5,NA_real_))
lmh14_v2 <- melt(
data = lmh14,
id.vars = c("ID","education_code","sex"),
measure.vars = c("question1","question2"),
variable.name = "Question",
value.name = "Response")
lmh14_v2
ID education_code sex Question Response
1: 1 20 F question1 NA
2: 2 50 M question1 1
3: 3 20 M question1 5
4: 4 60 M question1 3
5: 1 20 F question2 4
6: 2 50 M question2 3
7: 3 20 M question2 5
8: 4 60 M question2 NA