0

I have simple matrix and I'd like to create combinations of rows and columns for all values in matrix.

mat <- matrix(c(2, 4, 3, 1, 5, 7), nrow=3, ncol=2)
colnames(mat) <- c("col1","col2")
rownames(mat) <- c("row1","row2","row3")

And desired output:

cols  rows  value
col1  row1  2
col1  row2  4
col1  row3  3
col2  row1  1
col2  row2  5
col2  row3  7

Is there any simple and fast solution for that. Many thanks for any of your advice.

martinkabe
  • 1,079
  • 2
  • 12
  • 27
  • http://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format – jogo Mar 09 '17 at 13:26

1 Answers1

1

Using melt of the reshape2 package.

library(reshape2)
mat <- matrix(c(2, 4, 3, 1, 5, 7), nrow=3, ncol=2)
colnames(mat) <- c("col1","col2")
rownames(mat) <- c("row1","row2","row3")
melt(mat)
drmariod
  • 11,106
  • 16
  • 64
  • 110