How to convert a data frame of integer64 values to be a matrix?

后端 未结 1 1965
庸人自扰
庸人自扰 2020-12-19 02:24

I have a data frame consisting entirely of integer64 columns that I\'d like to convert to be a matrix.

library(bit64)
(dfr <- data.frame(x = as.integer64(         


        
相关标签:
1条回答
  • 2020-12-19 03:04

    For a raw vector, assigning the dim attribute directly seems to work:

    > z <- as.integer64(1:10)
    > z
    integer64
     [1] 1  2  3  4  5  6  7  8  9  10
    > dim(z) <- c(10, 1)
    > z
    integer64
          [,1]
     [1,] 1   
     [2,] 2   
     [3,] 3   
     [4,] 4   
     [5,] 5   
     [6,] 6   
     [7,] 7   
     [8,] 8   
     [9,] 9   
    [10,] 10  
    

    For a data frame, cbinding the columns also works:

    > df <- data.frame(x=as.integer64(1:5), y=as.integer64(6:10))
    > df
      x  y
    1 1  6
    2 2  7
    3 3  8
    4 4  9
    5 5 10
    > cbind(df$x, df$y)
    integer64
         [,1] [,2]
    [1,] 1    6   
    [2,] 2    7   
    [3,] 3    8   
    [4,] 4    9   
    [5,] 5    10  
    

    So, for an arbitrary number of columns, do.call is the way to go:

    > do.call(cbind, df)
    integer64
         x y 
    [1,] 1 6 
    [2,] 2 7 
    [3,] 3 8 
    [4,] 4 9 
    [5,] 5 10
    
    0 讨论(0)
提交回复
热议问题