Creating tuples from two vectors

后端 未结 4 2038
余生分开走
余生分开走 2020-12-31 00:46

If I have two vectors of the same length A<-c(5,10) and B<-c(7,13) how can I easily turn these two vectors into a single tuple vector i. e. <

4条回答
  •  情深已故
    2020-12-31 01:39

    Others have mentioned lists. I see other possibilities:

    cbind(A, B)  # makes a column-major  2x2-"vector"
    rbind(A, B)  # an row major 2x2-"vector" which could also be added to an array with `abind`
    

    It is also possible to preserve their "origins"

    AB <- cbind(A=A, B=B)
    array(c(AB,AB+10), c(2,2,2) )
    , , 1
         [,1] [,2]
    [1,]    5    7
    [2,]   10   13
    , , 2
         [,1] [,2]
    [1,]   15   17
    [2,]   20   23
    
    > abind( array(c(AB,AB+10), c(2,2,2) ), AB+20)
    , , 1
          A  B
    [1,]  5  7
    [2,] 10 13
    
    , , 2
          A  B
    [1,] 15 17
    [2,] 20 23
    
    , , 3
          A  B
    [1,] 25 27
    [2,] 30 33
    

提交回复
热议问题