| 42333 |
ripley |
1 |
% File src/library/base/man/matrix.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 77436 |
maechler |
3 |
% Copyright 1995-2019 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{matrix}
|
| 56186 |
murdoch |
7 |
\alias{matrix}
|
| 15168 |
pd |
8 |
\alias{as.matrix}
|
|
|
9 |
\alias{as.matrix.default}
|
|
|
10 |
\alias{as.matrix.data.frame}
|
|
|
11 |
\alias{is.matrix}
|
| 863 |
hornik |
12 |
\title{Matrices}
|
| 15168 |
pd |
13 |
\description{
|
|
|
14 |
\code{matrix} creates a matrix from the given set of values.
|
|
|
15 |
|
|
|
16 |
\code{as.matrix} attempts to turn its argument into a matrix.
|
|
|
17 |
|
|
|
18 |
\code{is.matrix} tests if its argument is a (strict) matrix.
|
|
|
19 |
}
|
| 2 |
r |
20 |
\usage{
|
| 36816 |
ripley |
21 |
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
|
|
|
22 |
dimnames = NULL)
|
|
|
23 |
|
| 40331 |
ripley |
24 |
as.matrix(x, \dots)
|
|
|
25 |
\method{as.matrix}{data.frame}(x, rownames.force = NA, \dots)
|
| 36816 |
ripley |
26 |
|
| 2 |
r |
27 |
is.matrix(x)
|
|
|
28 |
}
|
| 7081 |
pd |
29 |
\arguments{
|
| 53724 |
ripley |
30 |
\item{data}{an optional data vector (including a list or
|
| 59708 |
ripley |
31 |
\code{\link{expression}} vector). Non-atomic classed \R objects are
|
|
|
32 |
coerced by \code{\link{as.vector}} and all attributes discarded.}
|
| 40331 |
ripley |
33 |
\item{nrow}{the desired number of rows.}
|
|
|
34 |
\item{ncol}{the desired number of columns.}
|
|
|
35 |
\item{byrow}{logical. If \code{FALSE} (the default) the matrix is
|
|
|
36 |
filled by columns, otherwise the matrix is filled by rows.}
|
| 47054 |
ripley |
37 |
\item{dimnames}{A \code{\link{dimnames}} attribute for the matrix:
|
|
|
38 |
\code{NULL} or a \code{list} of length 2 giving the row and column
|
|
|
39 |
names respectively. An empty list is treated as \code{NULL}, and a
|
|
|
40 |
list of length one as row names. The list can be named, and the
|
|
|
41 |
list names will be used as names for the dimensions.}
|
| 7081 |
pd |
42 |
\item{x}{an \R object.}
|
| 40331 |
ripley |
43 |
\item{\dots}{additional arguments to be passed to or from methods.}
|
|
|
44 |
\item{rownames.force}{logical indicating if the resulting matrix
|
|
|
45 |
should have character (rather than \code{NULL})
|
|
|
46 |
\code{\link{rownames}}. The default, \code{NA}, uses \code{NULL}
|
|
|
47 |
rownames if the data frame has \sQuote{automatic} row.names or for a
|
|
|
48 |
zero-row data frame.}
|
| 7081 |
pd |
49 |
}
|
|
|
50 |
\details{
|
| 43019 |
ripley |
51 |
If one of \code{nrow} or \code{ncol} is not given, an attempt is
|
| 15168 |
pd |
52 |
made to infer it from the length of \code{data} and the other
|
| 43019 |
ripley |
53 |
parameter. If neither is given, a one-column matrix is returned.
|
| 2 |
r |
54 |
|
| 51217 |
ripley |
55 |
If there are too few elements in \code{data} to fill the matrix,
|
| 28014 |
ripley |
56 |
then the elements in \code{data} are recycled. If \code{data} has
|
|
|
57 |
length zero, \code{NA} of an appropriate type is used for atomic
|
| 30058 |
ripley |
58 |
vectors (\code{0} for raw vectors) and \code{NULL} for lists.
|
| 28014 |
ripley |
59 |
|
| 51217 |
ripley |
60 |
\code{is.matrix} returns \code{TRUE} if \code{x} is a vector and has a
|
| 74363 |
maechler |
61 |
\code{"\link{dim}"} attribute of length 2 and \code{FALSE} otherwise.
|
| 40675 |
ripley |
62 |
Note that a \code{\link{data.frame}} is \strong{not} a matrix by this
|
| 51217 |
ripley |
63 |
test. The function is generic: you can write methods to handle
|
| 40675 |
ripley |
64 |
specific classes of objects, see \link{InternalMethods}.
|
| 20507 |
ripley |
65 |
|
| 39790 |
maechler |
66 |
\code{as.matrix} is a generic function. The method for data frames
|
| 61523 |
ripley |
67 |
will return a character matrix if there is only atomic columns and any
|
|
|
68 |
non-(numeric/logical/complex) column, applying \code{\link{as.vector}}
|
|
|
69 |
to factors and \code{\link{format}} to other non-character columns.
|
|
|
70 |
Otherwise, the usual coercion hierarchy (logical < integer < double <
|
|
|
71 |
complex) will be used, e.g., all-logical data frames will be coerced
|
|
|
72 |
to a logical matrix, mixed logical-integer will give a integer matrix,
|
|
|
73 |
etc.
|
| 39790 |
maechler |
74 |
|
| 59712 |
ripley |
75 |
The default method for \code{as.matrix} calls \code{as.vector(x)}, and
|
| 66444 |
hornik |
76 |
hence e.g.\sspace{}coerces factors to character vectors.
|
| 59712 |
ripley |
77 |
|
| 39790 |
maechler |
78 |
When coercing a vector, it produces a one-column matrix, and
|
| 37826 |
ripley |
79 |
promotes the names (if any) of the vector to the rownames of the matrix.
|
| 61433 |
ripley |
80 |
|
| 51315 |
ripley |
81 |
\code{is.matrix} is a \link{primitive} function.
|
| 63806 |
ripley |
82 |
|
|
|
83 |
The \code{print} method for a matrix gives a rectangular layout with
|
|
|
84 |
dimnames or indices. For a list matrix, the entries of length not
|
|
|
85 |
one are printed in the form \samp{integer,7} indicating the type
|
|
|
86 |
and length.
|
| 2 |
r |
87 |
}
|
| 53657 |
ripley |
88 |
\note{
|
|
|
89 |
If you just want to convert a vector to a matrix, something like
|
| 55047 |
ripley |
90 |
\preformatted{ dim(x) <- c(nx, ny)
|
| 53657 |
ripley |
91 |
dimnames(x) <- list(row_names, col_names)
|
|
|
92 |
}
|
|
|
93 |
will avoid duplicating \code{x}.
|
|
|
94 |
}
|
| 24300 |
ripley |
95 |
\references{
|
|
|
96 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
97 |
\emph{The New S Language}.
|
| 47262 |
ripley |
98 |
Wadsworth & Brooks/Cole.
|
| 24300 |
ripley |
99 |
}
|
| 20507 |
ripley |
100 |
\seealso{
|
| 51217 |
ripley |
101 |
\code{\link{data.matrix}}, which attempts to convert to a numeric
|
|
|
102 |
matrix.
|
|
|
103 |
|
|
|
104 |
A matrix is the special case of a two-dimensional \code{\link{array}}.
|
| 77436 |
maechler |
105 |
Currently, only if the environment variable \code{"_R_CLASS_MATRIX_ARRAY_"} is set to a
|
|
|
106 |
non-empty string, i.e., \code{if(nzchar(Sys.getenv("_R_CLASS_MATRIX_ARRAY_")))} but then
|
|
|
107 |
From \R 4.0.0 on, \code{\link{inherits}(m, "array")} is true for a \code{matrix} \code{m}.
|
| 20507 |
ripley |
108 |
}
|
| 1323 |
maechler |
109 |
\examples{
|
|
|
110 |
is.matrix(as.matrix(1:10))
|
| 61168 |
ripley |
111 |
!is.matrix(warpbreaks) # data.frame, NOT matrix!
|
| 27712 |
ripley |
112 |
warpbreaks[1:10,]
|
| 61168 |
ripley |
113 |
as.matrix(warpbreaks[1:10,]) # using as.matrix.data.frame(.) method
|
| 31777 |
maechler |
114 |
|
| 61168 |
ripley |
115 |
## Example of setting row and column names
|
| 61150 |
ripley |
116 |
mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE,
|
| 44243 |
ripley |
117 |
dimnames = list(c("row1", "row2"),
|
|
|
118 |
c("C.1", "C.2", "C.3")))
|
| 31777 |
maechler |
119 |
mdat
|
| 1323 |
maechler |
120 |
}
|
| 286 |
maechler |
121 |
\keyword{array}
|
|
|
122 |
\keyword{algebra}
|