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