EVOLUTION-MANAGER
Edit File: matrix.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Matrices</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for matrix {base}"><tr><td>matrix {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Matrices</h2> <h3>Description</h3> <p><code>matrix</code> creates a matrix from the given set of values. </p> <p><code>as.matrix</code> attempts to turn its argument into a matrix. </p> <p><code>is.matrix</code> tests if its argument is a (strict) matrix. </p> <h3>Usage</h3> <pre> matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) as.matrix(x, ...) ## S3 method for class 'data.frame' as.matrix(x, rownames.force = NA, ...) is.matrix(x) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>data</code></td> <td> <p>an optional data vector (including a list or <code><a href="expression.html">expression</a></code> vector). Non-atomic classed <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> objects are coerced by <code><a href="vector.html">as.vector</a></code> and all attributes discarded.</p> </td></tr> <tr valign="top"><td><code>nrow</code></td> <td> <p>the desired number of rows.</p> </td></tr> <tr valign="top"><td><code>ncol</code></td> <td> <p>the desired number of columns.</p> </td></tr> <tr valign="top"><td><code>byrow</code></td> <td> <p>logical. If <code>FALSE</code> (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.</p> </td></tr> <tr valign="top"><td><code>dimnames</code></td> <td> <p>A <code><a href="dimnames.html">dimnames</a></code> attribute for the matrix: <code>NULL</code> or a <code>list</code> of length 2 giving the row and column names respectively. An empty list is treated as <code>NULL</code>, and a list of length one as row names. The list can be named, and the list names will be used as names for the dimensions.</p> </td></tr> <tr valign="top"><td><code>x</code></td> <td> <p>an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>additional arguments to be passed to or from methods.</p> </td></tr> <tr valign="top"><td><code>rownames.force</code></td> <td> <p>logical indicating if the resulting matrix should have character (rather than <code>NULL</code>) <code><a href="colnames.html">rownames</a></code>. The default, <code>NA</code>, uses <code>NULL</code> rownames if the data frame has ‘automatic’ row.names or for a zero-row data frame.</p> </td></tr> </table> <h3>Details</h3> <p>If one of <code>nrow</code> or <code>ncol</code> is not given, an attempt is made to infer it from the length of <code>data</code> and the other parameter. If neither is given, a one-column matrix is returned. </p> <p>If there are too few elements in <code>data</code> to fill the matrix, then the elements in <code>data</code> are recycled. If <code>data</code> has length zero, <code>NA</code> of an appropriate type is used for atomic vectors (<code>0</code> for raw vectors) and <code>NULL</code> for lists. </p> <p><code>is.matrix</code> returns <code>TRUE</code> if <code>x</code> is a vector and has a <code>"<a href="dim.html">dim</a>"</code> attribute of length 2 and <code>FALSE</code> otherwise. Note that a <code><a href="data.frame.html">data.frame</a></code> is <strong>not</strong> a matrix by this test. The function is generic: you can write methods to handle specific classes of objects, see <a href="InternalMethods.html">InternalMethods</a>. </p> <p><code>as.matrix</code> 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 <code><a href="vector.html">as.vector</a></code> to factors and <code><a href="format.html">format</a></code> 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. </p> <p>The default method for <code>as.matrix</code> calls <code>as.vector(x)</code>, and hence e.g. coerces factors to character vectors. </p> <p>When coercing a vector, it produces a one-column matrix, and promotes the names (if any) of the vector to the rownames of the matrix. </p> <p><code>is.matrix</code> is a <a href="Primitive.html">primitive</a> function. </p> <p>The <code>print</code> method for a matrix gives a rectangular layout with dimnames or indices. For a list matrix, the entries of length not one are printed in the form <span class="samp">integer,7</span> indicating the type and length. </p> <h3>Note</h3> <p>If you just want to convert a vector to a matrix, something like </p> <pre> dim(x) <- c(nx, ny) dimnames(x) <- list(row_names, col_names) </pre> <p>will avoid duplicating <code>x</code>. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="data.matrix.html">data.matrix</a></code>, which attempts to convert to a numeric matrix. </p> <p>A matrix is the special case of a two-dimensional <code><a href="array.html">array</a></code>. </p> <h3>Examples</h3> <pre> is.matrix(as.matrix(1:10)) !is.matrix(warpbreaks) # data.frame, NOT matrix! warpbreaks[1:10,] as.matrix(warpbreaks[1:10,]) # using as.matrix.data.frame(.) method ## Example of setting row and column names mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3"))) mdat </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>