如何将R中的因子转换为多个指标变量,每个级别一个?
答案 0 :(得分:8)
一种方法是使用model.matrix()
:
model.matrix(~Species, iris)
(Intercept) Speciesversicolor Speciesvirginica
1 1 0 0
2 1 0 0
3 1 0 0
...
148 1 0 1
149 1 0 1
150 1 0 1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$Species
[1] "contr.treatment"
答案 1 :(得分:4)
有几种方法可以实现,但您可以使用model.matrix
:
color <- factor(c("red","green","red","blue"))
data.frame(model.matrix(~color-1))
# colorblue colorgreen colorred
# 1 0 0 1
# 2 0 1 0
# 3 0 0 1
# 4 1 0 0
答案 2 :(得分:3)
如果我正确理解了您的问题,请使用model.matrix
命令,就像这样。
dd <- data.frame(a = gl(3,4), b = gl(4,1,12))
model.matrix(~ a + b, dd)
(Intercept) a2 a3 b2 b3 b4
1 1 0 0 0 0 0
2 1 0 0 1 0 0
3 1 0 0 0 1 0
4 1 0 0 0 0 1
5 1 1 0 0 0 0
6 1 1 0 1 0 0
7 1 1 0 0 1 0
8 1 1 0 0 0 1
9 1 0 1 0 0 0
10 1 0 1 1 0 0
11 1 0 1 0 1 0
12 1 0 1 0 0 1
attr(,"assign")
[1] 0 1 1 2 2 2
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"
attr(,"contrasts")$b
[1] "contr.treatment"
答案 3 :(得分:2)
试试这个:
myfactors<-factor(sample(c("f1","f2","f3"),10,replace=T));
myIndicators<-diag(nlevels(myfactors))[myfactors,];