给定一个长度为6、包含3个唯一数字的数组,如:
c (1, 1, 1, 2, 3, 3)
如何构造以下6x3矩阵:
[1, 0 , 0]
[1, 0 , 0]
[1, 0 , 0]
[0, 1 , 0]
[0, 0, 1]
[0, 0, 1]
一种朴素的方法是:
V = c(1,1,1,2,3,3)
Z = matrix(0,6,3)
for (i in 1:6) {Z[i, V[i]] = 1}
但是对于这种类型的操作是否有一个很好的一行符?
我们可以在base R
中使用model.matrix
model.matrix(~ . - 1, data.frame(v1 = factor(v1)))
与产出
v11 v12 v13
1 1 0 0
2 1 0 0
3 1 0 0
4 0 1 0
5 0 0 1
6 0 0 1
或者用dummy_cols
代替fastDummies
library(fastDummies)
dummy_cols(v1)[-1]
.data_1 .data_2 .data_3
1 1 0 0
2 1 0 0
3 1 0 0
4 0 1 0
5 0 0 1
6 0 0 1
数据v1 <- c(1,1,1,2,3,3)
与modelr:
library(modelr)
V = c(1,1,1,2,3,3)
V <- factor(V)
Z <- as.matrix(model_matrix(V , ~ V -1))
Z
V1 V2 V3
[1,] 1 0 0
[2,] 1 0 0
[3,] 1 0 0
[4,] 0 1 0
[5,] 0 0 1
[6,] 0 0 1
试试outer
:
V = c(1,1,1,2,3,3)
+outer(V, unique(V), `==`)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 1 0 0
## [3,] 1 0 0
## [4,] 0 1 0
## [5,] 0 0 1
## [6,] 0 0 1
我们可以vapply
atablulate
t(vapply(v1, (x) tabulate(x, max(v1)), numeric(max(v1))))
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 1 0 0
# [3,] 1 0 0
# [4,] 0 1 0
# [5,] 0 0 1
# [6,] 0 0 1
数据:
v1 <- c(1, 1, 1, 2, 3, 3)