数据:
m <- Matrix(c(0,0,2:0), 3,5)
rownames(m) <- paste0("g",1:3)
colnames(m) <- paste0("c",1:5)
解决方案:
data.frame(rows=rownames(x)[row(x)],cols=colnames(x)[col(x)],value=as.numeric(x))
这似乎比
reshape2
s
melt
被胁迫者
matrix
和
mefa4
s
Melt
:
mf <- function(x){
mefa4::Melt(x)
}
df <- function(x){
data.frame(rows=rownames(x)[row(x)],cols=colnames(x)[col(x)],value=as.numeric(x))
}
rs <- function(x){
reshape2::melt(Matrix::as.matrix(x))
}
microbenchmark::microbenchmark(df(m))
Unit: microseconds
expr min lq mean median uq max neval
df(m) 330.378 376.887 778.3489 476.903 699.743 14953.47 100
microbenchmark::microbenchmark(rs(m))
Unit: microseconds
expr min lq mean median uq max neval
rs(m) 537.328 638.683 1197.268 726.679 1042.261 15073.3 100
microbenchmark::microbenchmark(mf(m))
Unit: microseconds
expr min lq mean median uq max neval
mf(m) 459.947 526.6585 1023.575 677.9775 1007.214 6712.497 100