Link together sparse model matrices by row names
I am trying to plot a large sparse matrix using the split-apply-comb approach, separately calling sparse.model.matrix()
from the batch Matrix
on subsets of the columns of the dataframe and then concatenating them together into a complete matrix. I have to do this due to memory constraints (I cannot immediately call sparse.model.matrix on the whole df). This process works fine and I get a list of sparse matrices, but they are of different sizes and when I try to link them together I can't.
Example:
data(iris)
set.seed(100)
iris$v6 <- sample(c("a","b","c",NA), 150, replace=TRUE)
iris$v7 <- sample(c("x","y",NA), 150, replace = TRUE)
sparse_m1 <- sparse.model.matrix(~., iris[,1:5])
sparse_m2 <- sparse.model.matrix(~.-1, iris[, 6:7])
dim(sparse_m1)
[1] 150 7
dim(sparse_m2)
[1] 71 4
cbind2(sparse_m1, sparse_m2)
Error: Matrices must have same number of rows in cbind2(sparse_m1, sparse_m2)
cbind(sparse_m1, sparse_m2)
Error: Matrices must have same number of rows in cbind2(..1, r)
The matrices have the same row names, only some rows were omitted from sparse_m2 because they were missing values in both columns. Is there a way to combine them?
I also tried to use rbind.fill.matrix()
from a package plyr
, first transfer and then call it and then re-transpose, but then I lose the column names as the row names are ignored in the rbind.fill.matrix file.
Any ideas?
source to share
No one has answered this question yet
See similar questions:
or similar: