EVOLUTION-MANAGER
Edit File: rowsum.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: Give Column Sums of a Matrix or Data Frame, Based on a...</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 rowsum {base}"><tr><td>rowsum {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Give Column Sums of a Matrix or Data Frame, Based on a Grouping Variable</h2> <h3>Description</h3> <p>Compute column sums across rows of a numeric matrix-like object for each level of a grouping variable. <code>rowsum</code> is generic, with a method for data frames and a default method for vectors and matrices. </p> <h3>Usage</h3> <pre> rowsum(x, group, reorder = TRUE, ...) ## S3 method for class 'data.frame' rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...) ## Default S3 method: rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a matrix, data frame or vector of numeric data. Missing values are allowed. A numeric vector will be treated as a column vector.</p> </td></tr> <tr valign="top"><td><code>group</code></td> <td> <p>a vector or factor giving the grouping, with one element per row of <code>x</code>. Missing values will be treated as another group and a warning will be given.</p> </td></tr> <tr valign="top"><td><code>reorder</code></td> <td> <p>if <code>TRUE</code>, then the result will be in order of <code>sort(unique(group))</code>, if <code>FALSE</code>, it will be in the order that groups were encountered.</p> </td></tr> <tr valign="top"><td><code>na.rm</code></td> <td> <p>logical (<code>TRUE</code> or <code>FALSE</code>). Should <code>NA</code> (including <code>NaN</code>) values be discarded?</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>other arguments to be passed to or from methods</p> </td></tr> </table> <h3>Details</h3> <p>The default is to reorder the rows to agree with <code>tapply</code> as in the example below. Reordering should not add noticeably to the time except when there are very many distinct values of <code>group</code> and <code>x</code> has few columns. </p> <p>The original function was written by Terry Therneau, but this is a new implementation using hashing that is much faster for large matrices. </p> <p>To sum over all the rows of a matrix (ie, a single <code>group</code>) use <code><a href="colSums.html">colSums</a></code>, which should be even faster. </p> <p>For integer arguments, over/underflow in forming the sum results in <code>NA</code>. </p> <h3>Value</h3> <p>A matrix or data frame containing the sums. There will be one row per unique value of <code>group</code>. </p> <h3>See Also</h3> <p><code><a href="tapply.html">tapply</a></code>, <code><a href="../../stats/html/aggregate.html">aggregate</a></code>, <code><a href="colSums.html">rowSums</a></code> </p> <h3>Examples</h3> <pre> require(stats) x <- matrix(runif(100), ncol = 5) group <- sample(1:8, 20, TRUE) (xsum <- rowsum(x, group)) ## Slower versions tapply(x, list(group[row(x)], col(x)), sum) t(sapply(split(as.data.frame(x), group), colSums)) aggregate(x, list(group), sum)[-1] </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>