How to turn a vector into a matrix in R?

前端 未结 2 1727
臣服心动
臣服心动 2020-12-08 18:27

I have a vector with 49 numeric values. I want to have a 7x7 numeric matrix instead.

Is there some sort of convenient automatic conversion statement I can use, or d

2条回答
  •  旧时难觅i
    2020-12-08 18:50

    A matrix is really just a vector with a dim attribute (for the dimensions). So you can add dimensions to vec using the dim() function and vec will then be a matrix:

    vec <- 1:49
    dim(vec) <- c(7, 7)  ## (rows, cols)
    vec
    
    > vec <- 1:49
    > dim(vec) <- c(7, 7)  ## (rows, cols)
    > vec
         [,1] [,2] [,3] [,4] [,5] [,6] [,7]
    [1,]    1    8   15   22   29   36   43
    [2,]    2    9   16   23   30   37   44
    [3,]    3   10   17   24   31   38   45
    [4,]    4   11   18   25   32   39   46
    [5,]    5   12   19   26   33   40   47
    [6,]    6   13   20   27   34   41   48
    [7,]    7   14   21   28   35   42   49
    

提交回复
热议问题