Misconstruction
Misconstruction

Reputation: 1909

R: List of indices to binary matrix

Say I have a list of indices, like:

l <- list(c(1,2,3), c(1), c(1,5), c(2, 3, 5))

Which specify the non-zero elements in a matrix, like:

(m <- matrix(c(1,1,1,0,0, 1,0,0,0,0, 1,0,0,0,5, 0,1,1,0,1), nrow=4, byrow=TRUE))

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    0    0
[2,]    1    0    0    0    0
[3,]    1    0    0    0    5
[4,]    0    1    1    0    1

What is the fastest way, using R, to make m from l, giving that the matrix is very big, say 50.000 rows and 2000 columns?

Upvotes: 5

Views: 1539

Answers (2)

Johann de Jong
Johann de Jong

Reputation: 91

For me, the following is at least 3 times faster than the suggestions above, on data the size as specified in the question (5e4 x 2e3):

  unlist_l <- unlist(l)
  M <- matrix(0, nrow = length(l), ncol = max(unique(unlist_l)))
  ij <- cbind(rep(1:length(l), lengths(l)), unlist_l)
  M[ij] <- 1

Performance might depend on data size and degree of sparsity.

Upvotes: 1

akrun
akrun

Reputation: 887501

Try

d1 <- stack(setNames(l, seq_along(l)))
library(Matrix)
m1 <- sparseMatrix(as.numeric(d1[,2]), d1[,1], x=1)
as.matrix(m1)
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1    1    1    0    0
#[2,]    1    0    0    0    0
#[3,]    1    0    0    0    1
#[4,]    0    1    1    0    1

Or instead of stack, we could use melt

library(reshape2)
d2 <- melt(l)
sparseMatrix(d2[,2], d2[,1],x=1)

Or using only base R

Un1 <- unlist(l)
m1 <- matrix(0, nrow=length(l), ncol=max(Un1))
m1[cbind(as.numeric(d1$ind), d1$values)] <- 1
m1

Upvotes: 5

Related Questions