1

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]])
SabDeM
  • 7,050
  • 2
  • 25
  • 38
Nicolas
  • 105
  • 2
  • 9
  • I don't think that I would change anything in the sequence of entries stored in a sparse matrix. For an ordinary matrix or dataframe the solution below certainly works, but I'm unsure if the indexing of the non-zero elements in the dgCMatrix will still make sense after changing the order of the rows. – RHertel Jul 22 '15 at 10:55

2 Answers2

1

I found this solution on the site:

new_df <- df[ order(row.names(df)), ]

Link: How can I use the row.names attribute to order the rows of my dataframe in R?

Community
  • 1
  • 1
0

I added a dummy example for more clarity. Thanks for your answer Mikkel, it actually works using:

Z1 <- Matrix:::fac2sparse(dat$fac1, "d",drop=F)
Z1 <- Z1[order(row.names(Z1)),]

Z2 <- Matrix:::fac2sparse(dat$fac2, "d",drop=F)
Z2 <- Z2[order(row.names(Z2)),]

Z3 <- Matrix:::fac2sparse(dat$fac3, "d",drop=F)
Z3 <- Z3[order(row.names(Z3)),]

ZZ <- Z1+Z2+Z3

I found that an alternative solution was to sort the levels of the factors before using lapply:

dat$fac1 <- factor(dat$fac1,levels=sort(levels(dat$fac1))
dat$fac2 <- factor(dat$fac2,levels=sort(levels(dat$fac2))
dat$fac3 <- factor(dat$fac3,levels=sort(levels(dat$fac3))

Zl <- lapply(c("fac1","fac2","fac3"),function(nm) Matrix:::fac2sparse(dat[[nm]],"d",drop=F))

ZZ <- Reduce("+", Zl[-1], Zl[[1]])
Nicolas
  • 105
  • 2
  • 9