R: Recode nominal variables to binary variables

cat2bin {fpc} | R Documentation

## Recode nominal variables to binary variables

### Description

Recodes a dataset with nominal variables so that the nominal variables are replaced by binary variables for the categories.

### Usage

      cat2bin(x,categorical=NULL)

### Arguments

`x` | data matrix or data frame. The data need to be organised case-wise, i.e., if there are categorical variables only, and 15 cases with values c(1,1,2) on the 3 variables, the data matrix needs 15 rows with values 1 1 2. (Categorical variables could take numbers or strings or anything that can be coerced to factor levels as values.)
`categorical` | vector of numbers of variables to be recoded.

### Value

A list with components

`data` | data matrix with variables specified in `categorical` replaced by 0-1 variables, one for each category.
`variableinfo` | list of lists. One list for every variable in the original dataset, with four components each, namely `type` (`"categorical"` or `"not recoded"`), `levels` (levels of nominal recoded variables in order of binary variable in output dataset), `ncat` (number of categories for recoded variables), `varnum` (number of variables in output dataset belonging to this original variable).

### Author(s)

Christian Hennig c.hennig@ucl.ac.uk http://www.homepages.ucl.ac.uk/~ucakche

### See Also

`discrete.recode`

### Examples

      set.seed(776655)
      v1 <- rnorm(20)
      v2 <- rnorm(20)
      d1 <- sample(1:5,20,replace=TRUE)
      d2 <- sample(1:4,20,replace=TRUE)
      ldata <- cbind(v1,v2,d1,d2)
      lc <- cat2bin(ldata,categorical=3:4)

[Package fpc version 2.1-10 Index]