tonytonov
tonytonov

Reputation: 25608

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

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?

Upvotes: 12

Views: 3440

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

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

Upvotes: 12

Related Questions