1 minute read

Sometimes a categorical variable, or a factor has to be transformed to a binary matrix in order to run certain modeling or computational algorithms. In R, model.mtrix creates, from a factor, a set of indicator variables. Each level of the factor, or each category, becomes one column in the resulting matrix. If a row contains the level, the corresponding value of the column is 1 or 0 otherwise.

One factor

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjQXNzaWduIGNvbHVtbnMgbmFtZXMgZHVyaW5nIGNyZWF0aW9uXG54IDwtIGZhY3RvcihcbiAgeD1jKFwiY2FyXCIsIFwidHJhaW5cIiwgXCJiaWtlXCIsIFwiY2FyXCIsIFwiY2FyXCIpLFxuICBsZXZlbHM9YyhcImNhclwiLFwidHJhaW5cIixcImJpa2VcIixcIndhbGtcIiksXG4gIG9yZGVyZWQ9RkFMU0VcbikgXG5wcmludCh4KVxubW9kZWwubWF0cml4KH4geCAtIDEpIn0=

Multiple factors

diet <- factor(c(1,1,1,1,2,2,2,2))
sex <- factor(c("f","f","m","m","f","f","m","m"))
model.matrix(~ diet + sex -1)
##   diet1 diet2 sexm
## 1     1     0    0
## 2     1     0    0
## 3     1     0    1
## 4     1     0    1
## 5     0     1    0
## 6     0     1    0
## 7     0     1    1
## 8     0     1    1
## attr(,"assign")
## [1] 1 1 2
## attr(,"contrasts")
## attr(,"contrasts")$diet
## [1] "contr.treatment"
## 
## attr(,"contrasts")$sex
## [1] "contr.treatment"
comments powered by Disqus