There is something I do not understand in model.matrix. When I enter a single binary variable without an intercept it returns two levels.
> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x, data = temp.data)
> head(temp.data.table)
xA xB
1 1 0
2 0 1
3 0 1
4 0 1
5 1 0
6 0 1
However, when I enter another binary level, it creates only 3 columns. Why is that? What makes the behavior of the function suddenly different? and how can I avoid it?
> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+ 'y' = sample(c('J', 'D'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x + y, data = temp.data)
> head(temp.data.table)
xA xB yJ
1 0 1 0
2 0 1 1
3 0 1 1
4 0 1 0
5 1 0 1
6 0 1 0