How to turn a list of lists to a sparse matrix in R without using lapply? -
i have list of lists resulting bigsplit() operation (from package biganalytics, part of bigmemory packages).
each list represents column in matrix, , each list item index value of 1 in binary matrix.
what best way turn list sparse binary (0/1) matrix? using lapply() within lapply() solution? how keep factors naming lists names columns?
you might consider using matrix package deals large sparse matrices in more efficient way base r. can build sparse matrix of 0s , 1s describing rows , columns should 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,] . | .
Comments
Post a Comment