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

35,143

## Solution 1

`matrix` takes `data` and further arguments `nrow` and `ncol`.

``````?matrix
If one of ‘nrow’ or ‘ncol’ is not given, an attempt is made to
infer it from the length of ‘data’ and the other parameter.  If
neither is given, a one-column matrix is returned.
``````

`as.matrix` is a method with different behaviours for different types, but mainly to give back an n*m matrix from an n*m input.

``````?as.matrix
‘as.matrix’ is a generic function.  The method for data frames
will return a character matrix if there is only atomic columns and
any non-(numeric/logical/complex) column, applying ‘as.vector’ to
factors and ‘format’ to other non-character columns.  Otherwise,
the usual coercion hierarchy (logical < integer < double <
complex) will be used, e.g., all-logical data frames will be
coerced to a logical matrix, mixed logical-integer will give a
integer matrix, etc.
``````

The difference between them comes primarily from the shape of the input, `matrix` doesn't care about the shape, `as.matrix` does and will maintain it (though the details depend on the actual methods for the input, and in your case a dimensionless vector corresponds to a single column matrix.) It doesn't matter if the input is raw, logical, integer, numeric, character, or complex, etc.

## Solution 2

`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.

## Solution 3

matrix creates a matrix from the given set of values. as.matrix attempts to turn its argument into a matrix.

Further, `matrix()` makes efforts to keep logical matrices logical, i.e., and to determine specially structured matrices such as symmetric, triangular or diagonal ones.

`as.matrix` is a generic function. The method for data frames will return a character matrix if there is only atomic columns and any non-(numeric/logical/complex) column, applying `as.vector` to factors and format to other non-character columns. Otherwise, the usual coercion hierarchy `(logical < integer < double < complex)` will be used, e.g., all-logical data frames will be coerced to a logical matrix, mixed logical-integer will give a integer matrix, etc.

The default method for `as.matrix` calls `as.vector(x)`, and hence e.g. coerces factors to character vectors.

Share:
35,143

Author by

Updated on January 30, 2020

• Linda Rabady almost 4 years

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:

``````> a=c(1,2,3,4)
> a
[1] 1 2 3 4
> matrix(a)
[,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
> as.matrix(a)
[,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
``````
• Roland over 10 years
Read the documentation. E.g., compare the output of `DF <- data.frame(a=1:5,b=6:10); as.matrix(DF); matrix(DF)`.
• Linda Rabady over 10 years
yes but i am not dealing with data.frame i.e. my matrix is numerical data only.
• Roland over 10 years
You asked for the difference between these functions. The difference is documented and I showed you an example. That the functions can (under specific circumstances) give the same result has no impact on the answer to your question.
• Roland over 10 years
That is not true. Compare `matrix(matrix(1:10,2))` and `as.matrix(matrix(1:10,2))`.