Home > Mobile >  Loop over all 'items' with R
Loop over all 'items' with R

Time:09-01

I have a dataset based on a survey with over 85 questions. All questions were multiple choice with levels from 1 to 5 - and some questions had a text field.

I made a new dataset which only have the variables:

ID Education_code age sex 

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

d <- lmh14[, . (ID, education_code, age, sex, item = question1)]

lmh14 looked this before up to question 87:

 ID education_code    age sex  question1 question2 question3 question4 ..... question87
    1 20               87  F   NA
    2 50               67  M    1
    3 20               56  M    5
    4 60               52  M    3

Now the dataset looks like this

ID education_code age sex item 
1 20               87  F   NA
2 50               67  M    1
3 20               56  M    5
4 60               52  M    3

Here ID 4 is 52, male, who responded level 3 of question 1

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