multresp: Create an indicator matrix from a multiple response variable

Description Usage Arguments Value Author(s) Examples

View source: R/multresp.R

Description

A multiple reponse variable is one whose values consist of a list of possible choices. Such a variable can be represented in a linear model through a matrix of indicator variables.

Usage

1
multresp(x, split = "[ \t,]+", sep = ".", root = deparse(substitute(x)))

Arguments

x

a factor or character vector containing comma- or blank-separated lists of reponse choices for each subject. For example, each subject could have a list of symptoms: c('cough fever', 'cough bronchitis', 'headache wheeze', ”, 'cough high_fever', NA).

split

a regular expression matching the separator between fields. The default is "[ \t,]+" which matches one more blank, tab or comma. To use semicolons instead of commas as separators, use: "[ \t;]+" .

sep

the separator used to generated the column names of the indicator matrix. sep separates the root from the response values. Default: ".". For example, if x is the name of the variable, then the default column name for response value 'cough' is x.cough.

root

the root for column names. By default, the name of the variable. the

Value

A matrix of indicator variables. For, example, if x <-c('cough fever', 'cough bronchitis', 'headache wheeze', '', 'cough high_fever', NA) multresp(x) produces: x.bronchitis x.cough x.fever x.headache x.high_fever x.wheeze [1,] 0 1 1 0 0 0 [2,] 1 1 0 0 0 0 [3,] 0 0 0 1 0 1 [4,] 0 0 0 0 0 0 [5,] 0 1 0 0 1 0 [6,] 0 0 0 0 0 0

Author(s)

G. Monette (georges@yorku.ca)

Examples

1
2
3
4
5
6
x <-c('cough fever', 'cough bronchitis', 'headache wheeze', '', 'cough high_fever', NA)
multresp(x)

data <- data.frame(x = x, y = 1:6)
data <- with(data, cbind(data, multresp(x)))
data

gmonette/spida documentation built on May 17, 2019, 7:25 a.m.