The answers to this question document that the tidyverse analog of reshape2::melt() is tidyr::pivot_longer(). That's true, as far as it goes, but I still find the process of melting a matrix with defined dimnames in tidyverse much less convenient than the tidyverse equivalent.
Example:
z <- matrix(1:12, ncol=3,
dimnames = list(a = 1:4, b = LETTERS[1:3]))
reshape2::melt(z) automatically gives me a data frame with the ID columns named "a" and "b", corresponding to the elements of names(dimnames(z)).
I originally had a somewhat clunky solution here but realized it didn't work. I think the required steps are
- store the row-dimension name and column-dimension names
- convert to a data frame
- add the rowname as a column with name "tmp" (with
tibble::rownames_to_column("tmp")) pivot_longer()with-tmpand setnames_toto the column-dimension name- rename "tmp" to the row_dimension name (could save a step if we did NSE magic)
This seems much clunkier than melt(). Obviously I could write a utility function to encapsulate this, but I'm wondering if I'm missing a solution that is more compact and/or tidyverse-idiomatic. What do other people do when the first step in their data pipeline is a matrix?