我有以下形式的数据,我想从这些数据创建一个矩阵。
B<- c('nancy','bill','bob','badri','bill')
c<- c('martial-arts','dance','sports','judo','judo')
df<- data.frame(B,C)
我想创建一个属于哪个组并且用户为row.names的矩阵。有人可以有任何建议吗?
user martial-arts dance sports judo
nancy 1 0 0 0
bill 0 1 0 1
bob 0 0 1 0
badri 0 0 0 1
答案 0 :(得分:2)
也许是这样的:
x <- c('nancy','bill','bob','badri','bill')
y <- c('martial-arts','dance','sports','judo','judo')
x0 <- unique(x); y0 <- unique(y)
mat <- matrix(0L, length(x0), length(y0), dimnames = list(x0, y0))
mat[cbind(match(x, x0), match(y, y0))] <- 1L
# martial-arts dance sports judo
#nancy 1 0 0 0
#bill 0 1 0 1
#bob 0 0 1 0
#badri 0 0 0 1
我使用了矩阵索引:
match(x, x0)
提供行索引; match(y, y0)
提供列索引; cbind(match(x, x0), match(y, y0))
给出矩阵索引,其中1为。如果您知道结果矩阵的零比零多,则可以构造一个稀疏矩阵:
library(Matrix)
sparseMatrix(i = match(x, x0), j = match(y, y0), x = 1, dimnames = list(x0, y0))
#4 x 4 sparse Matrix of class "dgCMatrix"
# martial-arts dance sports judo
#nancy 1 . . .
#bill . 1 . 1
#bob . . 1 .
#badri . . . 1
@thelatemail替代方案:
## coding to factor with desired order of levels is necessary
x <- factor(x, levels = x0)
y <- factor(y, levels = y0)
## dense matrix
xtabs(~ x + y)
# y
#x martial-arts dance sports judo
# nancy 1 0 0 0
# bill 0 1 0 1
# bob 0 0 1 0
# badri 0 0 0 1
## sparse matrix
xtabs(~ x + y, sparse = TRUE)
#4 x 4 sparse Matrix of class "dgCMatrix"
# martial-arts dance sports judo
#nancy 1 . . .
#bill . 1 . 1
#bob . . 1 .
#badri . . . 1