Reputation: 1139
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.
Upvotes: 0
Views: 1091
Reputation: 11772
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)
Upvotes: 1