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

前端 未结 3 704
自闭症患者
自闭症患者 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:48

    depening on your desired output (since you are tryning to convert multiple dimension into a 'flat' table), here is a possible solution using the plyr-package:

    plyr's adply takes a array, and retuns a data.frame that you can easily convert to a data.table

    library(plyr)
    dt <- setDT( adply( aaa, c(1,2) ) )
    
        X1 X2         V1          V2
     1:  1  1 -0.5869804  1.30996405
     2:  2  1  1.3398003  1.87641841
     3:  3  1 -0.3268114 -0.12771971
     4:  1  2  0.8966523 -1.38669407
     5:  2  2 -0.4612773 -1.48036434
     6:  3  2 -0.6798351 -0.09369933
     7:  1  3  0.1311092  0.40458169
     8:  2  3 -1.7098850  0.39616792
     9:  3  3 -0.4589561 -1.14020015
    10:  1  4  0.5348955 -0.25779528
    11:  2  4  0.7099319  0.19067120
    12:  3  4 -0.1545822 -0.75378610
    

提交回复
热议问题