How to convert array to data.table in R and back?

前端 未结 3 693
自闭症患者
自闭症患者 2021-01-20 06:19

is this the most straightforward way to convert an array into a data.table?

require(data.table)
require(ggplot2)

# this returns a data.tabl         


        
3条回答
  •  误落风尘
    2021-01-20 06:55

    only works for versions 1.11.4 and 1.11.2 but not for some earlier versions

    both approaches essentially return the same data.table but with A=1, B=2, C=3 in your second approach, and rows ordered in different ways. so the second approach is the way to go.

    DT2 <- as.data.table(aaa)
    head(DT2)
    #   V1 V2 V3       value
    #1:  1  1  1  0.32337516
    #2:  1  1  2  1.59189589
    #3:  1  2  1 -1.48751756
    #4:  1  2  2 -0.86749305
    #5:  1  3  1  0.01017255
    #6:  1  3  2  2.66571093
    
    #compare
    DT[order(Freq), ]
    #and 
    DT2[order(value), ]
    

提交回复
热议问题