I have three dgCMatrix sparse matrices built using the Matrix package. The rows of these two matrices are not in the same order, so I would like to reorder them by rownames so that I can add the three matrices together. Would anyone have a hint about a quick way to do this?
Thanks a lot,
Nicolas
Here is a small example, where ZZ is wrong because the matrices are not in the same order:
dat <-data.frame(fac1=factor(c("small","large"),levels=c("small","large","medium")),fac2=factor(c("medium","large"),levels=c("medium","large","small")),fac3=factor(c("small","medium"),levels=c("small","medium","large")))
Zl <- lapply(c("fac1","fac2","fac3"), function(nm) Matrix:::fac2sparse(dat[[nm]], "d",drop=F))
ZZ <- Reduce("+", Zl[-1], Zl[[1]])