EVOLUTION-MANAGER
Edit File: by.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: Apply a Function to a Data Frame Split by Factors</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 by {base}"><tr><td>by {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Apply a Function to a Data Frame Split by Factors</h2> <h3>Description</h3> <p>Function <code>by</code> is an object-oriented wrapper for <code><a href="tapply.html">tapply</a></code> applied to data frames. </p> <h3>Usage</h3> <pre> by(data, INDICES, FUN, ..., simplify = TRUE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>data</code></td> <td> <p>an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object, normally a data frame, possibly a matrix.</p> </td></tr> <tr valign="top"><td><code>INDICES</code></td> <td> <p>a factor or a list of factors, each of length <code>nrow(data)</code>.</p> </td></tr> <tr valign="top"><td><code>FUN</code></td> <td> <p>a function to be applied to (usually data-frame) subsets of <code>data</code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments to <code>FUN</code>.</p> </td></tr> <tr valign="top"><td><code>simplify</code></td> <td> <p>logical: see <code><a href="tapply.html">tapply</a></code>.</p> </td></tr> </table> <h3>Details</h3> <p>A data frame is split by row into data frames subsetted by the values of one or more factors, and function <code>FUN</code> is applied to each subset in turn. </p> <p>For the default method, an object with dimensions (e.g., a matrix) is coerced to a data frame and the data frame method applied. Other objects are also coerced to a data frame, but <code>FUN</code> is applied separately to (subsets of) each column of the data frame. </p> <h3>Value</h3> <p>An object of class <code>"by"</code>, giving the results for each subset. This is always a list if <code>simplify</code> is false, otherwise a list or array (see <code><a href="tapply.html">tapply</a></code>). </p> <h3>See Also</h3> <p><code><a href="tapply.html">tapply</a></code>, <code><a href="lapply.html">simplify2array</a></code>. <code><a href="../../stats/html/ave.html">ave</a></code> also applies a function block-wise. </p> <h3>Examples</h3> <pre> require(stats) by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary) by(warpbreaks[, 1], warpbreaks[, -1], summary) by(warpbreaks, warpbreaks[,"tension"], function(x) lm(breaks ~ wool, data = x)) ## now suppose we want to extract the coefficients by group tmp <- with(warpbreaks, by(warpbreaks, tension, function(x) lm(breaks ~ wool, data = x))) sapply(tmp, coef) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>