What is the difference between matrix() and as.matrix() in r?

后端 未结 3 1640
死守一世寂寞
死守一世寂寞 2021-02-04 09:01

I ran the following in R and received the same output for both matrix() and as.matrix() and now I am not sure what the difference between them is:

3条回答
  •  一生所求
    2021-02-04 09:37

    matrix constructs a matrix from its first argument, with a given number of rows and columns. If the supplied object isn't large enough for the desired output, matrix will recycle its elements: for example, matrix(1:2), nrow=3, ncol=4). Conversely, if the object is too big, then the surplus elements will be dropped: for example, matrix(1:20, nrow=3, ncol=4).

    as.matrix converts its first argument into a matrix, the dimensions of which will be inferred from the input.

提交回复
热议问题