Home > Software engineering >  Loop over all 'test' with R
Loop over all 'test' with R

Time:09-01

I made a new dataset which only have the variables:

I have then added a new variable to my data set, called Item:

d <- test[, . (ID, age, test= test)]

My question is now, that I want to make a function that will make a loop over all questions i have ? is that possible?

CodePudding user response:

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

CodePudding user response:

Although the assign function is not the best, here is a loop that can do this for you.

for (Question in 1:2) {
  assign(
    x = paste0("Question",Question),
    value = lmh14[,.(ID, education_code, age, sex, item = get(paste0("question",Question)))])
}
  • Related