【发布时间】:2013-02-17 13:55:34
【问题描述】:
如何将 R 中的一个因子转换为多个指标变量,每个级别一个?
【问题讨论】:
标签: r
如何将 R 中的一个因子转换为多个指标变量,每个级别一个?
【问题讨论】:
标签: r
一种方法是使用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,否则生成的矩阵中会缺少级别。
n-1 虚拟变量来表示n 变量。因此,在 iris$Species 示例中,0 和 0 的级别表示物种为 Setosa。
有几种方法可以做到,但你可以使用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
【讨论】:
如果我正确理解了您的问题,请使用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"
【讨论】:
试试这个:
myfactors<-factor(sample(c("f1","f2","f3"),10,replace=T));
myIndicators<-diag(nlevels(myfactors))[myfactors,];
【讨论】: