将行转换为列以创建矩阵

converting rows to columns to create a matrix

我有以下形式的数据,我想根据这些数据创建一个矩阵。

 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

也许是这样的:

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