0

After downloading answers from Google Forms survey a problem related to multiple choice question emerged.

All answers to this question were coded in one variable/column and are separated by a comma. Unfortunatelly, I need to change them into 0-1 variables for each possible option. I managed to divide this "one variable" into different columns, but a specific answer/option can be found in various columns.

The code that I am using to create 0-1 variables is:

data$option1 <- ifelse(column1=='option1' || column2=='option1' || column3=='option1',1,0)

but it is very time-consuming as I have tens of columns and options.

Is there any way to do it in a loop using vectors with columns and options names? For example:

A <- c("column1", "column2", "column3", .....) 
B <- c("option1", "option2", "option3", .....)
for (b in B){
  for (a in A) {
Data$b <- ifelse (Data$a=='b',1,0)
}
}

I'm a beginner and I'm not sure how it should be done. I'll appreciate your help!

  • 1
    Please show a proper small reproducible example and expected output – akrun Aug 07 '16 at 11:47
  • Mateusz: There's a way to do this without separating the initial column (let's call it "col0") in multiple columns. Create a vector of options (as your vector `B` above then run: `for (opt in B) { Data[[opt]] <- as.numeric(grepl(opt, Data$col0, fixed = TRUE)) }` Here, `grepl` returns true/false for each row depending on whether or not the `col0` column contains the string `opt`. Then `as.numeric` converts it to 1/0. That works as long as none of your options' names contains another option's name. – Philippe Marchand Aug 07 '16 at 12:16
  • @PhilippeMarchand Let the OP edit with a reproducible example – akrun Aug 07 '16 at 12:18

0 Answers0