0

I've got matrix like this:

    a    b    c
x   1    2    3
y   3    3    2

I need to transform it to data.frame like below

x a 1 
x b 2 
x c 3 
y a 3 
y b 3 
y c 2 

I know that split() should work, but I can not set a,b,c as second names of rows. What should I do?

anba
  • 543
  • 1
  • 4
  • 7

1 Answers1

1

You can use as.data.frame(as.table()):

m <- matrix(c(1,3,2,3,3,2), nrow = 2)
rownames(m) <- c("x", "y")
colnames(m) <- letters[1:3]

  a b c
x 1 2 3
y 3 3 2

as.data.frame(as.table(m))

  Var1 Var2 Freq
1    x    a    1
2    y    a    3
3    x    b    2
4    y    b    3
5    x    c    3
6    y    c    2
LAP
  • 6,605
  • 2
  • 15
  • 28