如何将 R 中的一个因子转换为多个指标变量,每个级别一个?
问问题
9950 次
4 回答
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"
于 2013-02-17T14:10:36.100 回答
5
有几种方法可以做到这一点,但您可以使用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
于 2013-02-17T14:11:15.240 回答
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"
于 2013-02-17T14:15:19.113 回答
2
尝试这个:
myfactors<-factor(sample(c("f1","f2","f3"),10,replace=T));
myIndicators<-diag(nlevels(myfactors))[myfactors,];
于 2013-02-17T14:11:03.577 回答