EVOLUTION-MANAGER
Edit File: merge.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: Merge Two Data Frames</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 merge {base}"><tr><td>merge {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Merge Two Data Frames</h2> <h3>Description</h3> <p>Merge two data frames by common columns or row names, or do other versions of database <em>join</em> operations. </p> <h3>Usage</h3> <pre> merge(x, y, ...) ## Default S3 method: merge(x, y, ...) ## S3 method for class 'data.frame' merge(x, y, by = intersect(names(x), names(y)), by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE, incomparables = NULL, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, y</code></td> <td> <p>data frames, or objects to be coerced to one.</p> </td></tr> <tr valign="top"><td><code>by, by.x, by.y</code></td> <td> <p>specifications of the columns used for merging. See ‘Details’.</p> </td></tr> <tr valign="top"><td><code>all</code></td> <td> <p>logical; <code>all = L</code> is shorthand for <code>all.x = L</code> and <code>all.y = L</code>, where <code>L</code> is either <code><a href="logical.html">TRUE</a></code> or <code>FALSE</code>.</p> </td></tr> <tr valign="top"><td><code>all.x</code></td> <td> <p>logical; if <code>TRUE</code>, then extra rows will be added to the output, one for each row in <code>x</code> that has no matching row in <code>y</code>. These rows will have <code>NA</code>s in those columns that are usually filled with values from <code>y</code>. The default is <code>FALSE</code>, so that only rows with data from both <code>x</code> and <code>y</code> are included in the output.</p> </td></tr> <tr valign="top"><td><code>all.y</code></td> <td> <p>logical; analogous to <code>all.x</code>.</p> </td></tr> <tr valign="top"><td><code>sort</code></td> <td> <p>logical. Should the result be sorted on the <code>by</code> columns?</p> </td></tr> <tr valign="top"><td><code>suffixes</code></td> <td> <p>a character vector of length 2 specifying the suffixes to be used for making unique the names of columns in the result which are not used for merging (appearing in <code>by</code> etc).</p> </td></tr> <tr valign="top"><td><code>no.dups</code></td> <td> <p>logical indicating that <code>suffixes</code> are appended in more cases to avoid duplicated column names in the result. This was implicitly false before <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> version 3.5.0.</p> </td></tr> <tr valign="top"><td><code>incomparables</code></td> <td> <p>values which cannot be matched. See <code><a href="match.html">match</a></code>. This is intended to be used for merging on one column, so these are incomparable values of that column.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to be passed to or from methods.</p> </td></tr> </table> <h3>Details</h3> <p><code>merge</code> is a generic function whose principal method is for data frames: the default method coerces its arguments to data frames and calls the <code>"data.frame"</code> method. </p> <p>By default the data frames are merged on the columns with names they both have, but separate specifications of the columns can be given by <code>by.x</code> and <code>by.y</code>. The rows in the two data frames that match on the specified columns are extracted, and joined together. If there is more than one match, all possible matches contribute one row each. For the precise meaning of ‘match’, see <code><a href="match.html">match</a></code>. </p> <p>Columns to merge on can be specified by name, number or by a logical vector: the name <code>"row.names"</code> or the number <code>0</code> specifies the row names. If specified by name it must correspond uniquely to a named column in the input. </p> <p>If <code>by</code> or both <code>by.x</code> and <code>by.y</code> are of length 0 (a length zero vector or <code>NULL</code>), the result, <code>r</code>, is the <em>Cartesian product</em> of <code>x</code> and <code>y</code>, i.e., <code>dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y))</code>. </p> <p>If <code>all.x</code> is true, all the non matching cases of <code>x</code> are appended to the result as well, with <code>NA</code> filled in the corresponding columns of <code>y</code>; analogously for <code>all.y</code>. </p> <p>If the columns in the data frames not used in merging have any common names, these have <code>suffixes</code> (<code>".x"</code> and <code>".y"</code> by default) appended to try to make the names of the result unique. If this is not possible, an error is thrown. </p> <p>If a <code>by.x</code> column name matches one of <code>y</code>, and if <code>no.dups</code> is true (as by default), the y version gets suffixed as well, avoiding duplicate column names in the result. </p> <p>The complexity of the algorithm used is proportional to the length of the answer. </p> <p>In SQL database terminology, the default value of <code>all = FALSE</code> gives a <em>natural join</em>, a special case of an <em>inner join</em>. Specifying <code>all.x = TRUE</code> gives a <em>left (outer) join</em>, <code>all.y = TRUE</code> a <em>right (outer) join</em>, and both (<code>all = TRUE</code>) a <em>(full) outer join</em>. DBMSes do not match <code>NULL</code> records, equivalent to <code>incomparables = NA</code> in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span>. </p> <h3>Value</h3> <p>A data frame. The rows are by default lexicographically sorted on the common columns, but for <code>sort = FALSE</code> are in an unspecified order. The columns are the common columns followed by the remaining columns in <code>x</code> and then those in <code>y</code>. If the matching involved row names, an extra character column called <code>Row.names</code> is added at the left, and in all cases the result has ‘automatic’ row names. </p> <h3>Note</h3> <p>This is intended to work with data frames with vector-like columns: some aspects work with data frames containing matrices, but not all. </p> <p>Currently long vectors are not accepted for inputs, which are thus restricted to less than 2^31 rows. That restriction also applies to the result for 32-bit platforms. </p> <h3>See Also</h3> <p><code><a href="data.frame.html">data.frame</a></code>, <code><a href="by.html">by</a></code>, <code><a href="cbind.html">cbind</a></code>. </p> <p><code><a href="../../stats/html/dendrogram.html">dendrogram</a></code> for a class which has a <code>merge</code> method. </p> <h3>Examples</h3> <pre> authors <- data.frame( ## I(*) : use character columns of names to get sensible sort order surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")), nationality = c("US", "Australia", "US", "UK", "Australia"), deceased = c("yes", rep("no", 4))) authorN <- within(authors, { name <- surname; rm(surname) }) books <- data.frame( name = I(c("Tukey", "Venables", "Tierney", "Ripley", "Ripley", "McNeil", "R Core")), title = c("Exploratory Data Analysis", "Modern Applied Statistics ...", "LISP-STAT", "Spatial Statistics", "Stochastic Simulation", "Interactive Data Analysis", "An Introduction to R"), other.author = c(NA, "Ripley", NA, NA, NA, NA, "Venables & Smith")) (m0 <- merge(authorN, books)) (m1 <- merge(authors, books, by.x = "surname", by.y = "name")) m2 <- merge(books, authors, by.x = "name", by.y = "surname") stopifnot(exprs = { identical(m0, m2[, names(m0)]) as.character(m1[, 1]) == as.character(m2[, 1]) all.equal(m1[, -1], m2[, -1][ names(m1)[-1] ]) identical(dim(merge(m1, m2, by = NULL)), c(nrow(m1)*nrow(m2), ncol(m1)+ncol(m2))) }) ## "R core" is missing from authors and appears only here : merge(authors, books, by.x = "surname", by.y = "name", all = TRUE) ## example of using 'incomparables' x <- data.frame(k1 = c(NA,NA,3,4,5), k2 = c(1,NA,NA,4,5), data = 1:5) y <- data.frame(k1 = c(NA,2,NA,4,5), k2 = c(NA,NA,3,4,5), data = 1:5) merge(x, y, by = c("k1","k2")) # NA's match merge(x, y, by = "k1") # NA's match, so 6 rows merge(x, y, by = "k2", incomparables = NA) # 2 rows </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>