Converting a numeric matrix into a data.table (or data.frame)

后端 未结 3 1365
一个人的身影
一个人的身影 2021-02-04 11:02

Hoping there\'s a simple answer here but I can\'t find it anywhere.

I have a numeric matrix with labelled rows and columns:

     1    2    3    4
a    6          


        
3条回答
  •  星月不相逢
    2021-02-04 11:37

    The as.table and as.data.frame functions together will do this:

    > m <- matrix( sample(1:12), nrow=4 )
    > dimnames(m) <- list( One=letters[1:4], Two=LETTERS[1:3] )
    > as.data.frame( as.table(m) )
       One Two Freq
    1    a   A    7
    2    b   A    2
    3    c   A    1
    4    d   A    5
    5    a   B    9
    6    b   B    6
    7    c   B    8
    8    d   B   10
    9    a   C   11
    10   b   C   12
    11   c   C    3
    12   d   C    4
    

提交回复
热议问题