R/table.multi.R
multi.split.Rd
Split a multiple choices variable in a series of binary variables
multi.split(var, split.char = "/", mnames = NULL)
variable to split
character to split at
names to give to the produced variabels. If NULL, the name are computed from the original variable name and the answers.
Returns a data frame.
This function takes as input a multiple choices variable where choices are recorded as a string and separated with a fixed character. For example, if the question is about the favourite colors, answers could be "red/blue", "red/green/yellow", etc. This function splits the variable into as many variables as the number of different choices. Each of these variables as a 1 or 0 value corresponding to the choice of this answer. They are returned as a data frame.
v <- c("red/blue","green","red/green","blue/red")
multi.split(v)
#> v.blue v.red v.green
#> 1 1 1 0
#> 2 0 0 1
#> 3 0 1 1
#> 4 1 1 0
## One-way frequency table of the result
multi.table(multi.split(v))
#> n %multi
#> v.blue 2 50
#> v.red 3 75
#> v.green 2 50