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]])
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?
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]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.