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

后端 未结 3 1356
一个人的身影
一个人的身影 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:40

    Use melt from reshape2:

    library(reshape2)
    #Fake data
    x <- matrix(1:12, ncol = 3)
    colnames(x) <- letters[1:3]
    rownames(x) <- 1:4
    x.m <- melt(x)
    x.m
    
       Var1 Var2 value
    1     1    a     1
    2     2    a     2
    3     3    a     3
    4     4    a     4
    ...
    

提交回复
热议问题