Search code examples
rsparse-matrix

How to turn a list of lists to a sparse matrix in R without using lapply?


I have a list of lists resulting from a bigsplit() operation (from package biganalytics, part of the bigmemory packages).

Each list represents a column in a matrix, and each list item is an index to a value of 1 in a binary matrix.

What is the best way to turn this list into a sparse binary (0/1) matrix? Is using lapply() within an lapply() the only solution? How do I keep the factors naming the lists as names for the columns?


Solution

  • You might also consider using the Matrix package which deals with large sparse matrices in a more efficient way than base R. You can build a sparse matrix of 0s and 1s by describing which rows and columns should be 1s.

    library(Matrix)
    Test <- list(
        col1=list(2,4,7),
        col2=list(3,2,6,8),
        col3=list(1,4,5,3,7)
    )
    n.ids <- sapply(Test,length)
    vals <- unlist(Test)
    out <- sparseMatrix(vals, rep(seq_along(n.ids), n.ids))
    

    The result is

    > out
    8 x 3 sparse Matrix of class "ngCMatrix"
    
    [1,] . . |
    [2,] | | .
    [3,] . | |
    [4,] | . |
    [5,] . . |
    [6,] . | .
    [7,] | . |
    [8,] . | .