How to convert a data frame to a 3d array in R

后端 未结 3 1057
天涯浪人
天涯浪人 2020-12-16 16:33

I have a data frame that I want to convert to a three-dimensional array. One of the columns in the data frame should serve as the grouping variable for splitting the frame i

3条回答
  •  时光说笑
    2020-12-16 17:26

    Here is what I'd probably do:

    library(abind)
    abind(split(df, df$i), along=3)
    # , , 1
    # 
    #   i x y           l
    # 5 1 0 0 -0.56047565
    # 6 1 0 1 -0.23017749
    # 7 1 1 0  1.55870831
    # 8 1 1 1  0.07050839
    # 
    # , , 2
    # 
    #   i x y          l
    # 5 2 0 0  0.1292877
    # 6 2 0 1  1.7150650
    # 7 2 1 0  0.4609162
    # 8 2 1 1 -1.2650612
    

提交回复
热议问题