tidyr::gather vs. reshape2::melt on matrices

房东的猫 提交于 2019-11-30 23:45:06

问题


I've been a long time user of reshape2::melt in a rather non-standard way: I'm running numeric experiments and get a matrix as a result. I then melt it and produce some images.

Inspired by the similarity between reshape2 and tidyr, I'm now trying to achieve identical output on objects of class matrix. No luck so far:

library(reshape2)
library(tidyr)

set.seed(42)
mat <- matrix(runif(6), 3)
mat2 <- mat
colnames(mat2) <- letters[1:2]
rownames(mat2) <- letters[3:5]

melt(mat)
melt(mat2)
gather(mat) # fails
gather(mat2) # fails

Note that melt is smart and keeps dimnames if they are present. I've learnt how it works, so I can potentially add the following function to the method dispatch:

gather.matrix <- function(mat) {
  if (is.null(dimnames(mat))) {
    grid <- expand.grid(seq.int(nrow(mat)), seq.int(ncol(mat)))
  } else {
    grid <- expand.grid(dimnames(mat))
  }
  cbind(grid, value = as.vector(mat))
}

all.equal(melt(mat), 
          gather.matrix(mat))
#[1] TRUE
all.equal(melt(mat2), 
          gather.matrix(mat2))
#[1] TRUE

But the question is, can I force gather act in the same way as melt in my case? Is there any combination of parameters that would produce the desired output on mat and mat2?


回答1:


Perhaps a better answer will emerge, but in the meantime, I'll convert my comments to an answer:

Quoting from the README to "tidyr":

Note that tidyr is designed for use in conjunction with dplyr, so you should always load both.

... and from the README to "dplyr":

dplyr is the next iteration of plyr, focussed on tools for working with data frames (hence the d in the name).

As such, it sort of makes sense to not have methods for matrices.


Since gather already wraps around melt, if you really wanted a matrix method, you can save yourself writing a custom function and just do something like:

gather.matrix <- reshape2:::melt.matrix


来源:https://stackoverflow.com/questions/29429421/tidyrgather-vs-reshape2melt-on-matrices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!