merge function - RDocumentation (2024)

base (version 3.6.2)

Description

Merge two data frames by common columns or row names, or do other versions of database join operations.

Usage

merge(x, y, …)

# S3 method for defaultmerge(x, y, …)

# S3 method for data.framemerge(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, …)

Arguments

x, y

data frames, or objects to be coerced to one.

by, by.x, by.y

specifications of the columns used for merging. See ‘Details’.

all.x

logical; if TRUE, then extra rows will be added to the output, one for each row in x that has no matching row in y. These rows will have NAs in those columns that are usually filled with values from y. The default is FALSE, so that only rows with data from both x and y are included in the output.

all.y

logical; analogous to all.x.

sort

logical. Should the result be sorted on the by columns?

suffixes

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 by etc).

no.dups

logical indicating that suffixes are appended in more cases to avoid duplicated column names in the result. This was implicitly false before R version 3.5.0.

incomparables

values which cannot be matched. See match. This is intended to be used for merging on one column, so these are incomparable values of that column.

arguments to be passed to or from methods.

Value

A data frame. The rows are by default lexicographically sorted on the common columns, but for sort = FALSE are in an unspecified order. The columns are the common columns followed by the remaining columns in x and then those in y. If the matching involved row names, an extra character column called Row.names is added at the left, and in all cases the result has ‘automatic’ row names.

Details

merge is a generic function whose principal method is for data frames: the default method coerces its arguments to data frames and calls the "data.frame" method.

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 by.x and by.y. 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 match.

Columns to merge on can be specified by name, number or by a logical vector: the name "row.names" or the number 0 specifies the row names. If specified by name it must correspond uniquely to a named column in the input.

If by or both by.x and by.y are of length 0 (a length zero vector or NULL), the result, r, is the Cartesian product of x and y, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).

If all.x is true, all the non matching cases of x are appended to the result as well, with NA filled in the corresponding columns of y; analogously for all.y.

If the columns in the data frames not used in merging have any common names, these have suffixes (".x" and ".y" by default) appended to try to make the names of the result unique. If this is not possible, an error is thrown.

If a by.x column name matches one of y, and if no.dups is true (as by default), the y version gets suffixed as well, avoiding duplicate column names in the result.

The complexity of the algorithm used is proportional to the length of the answer.

In SQL database terminology, the default value of all = FALSE gives a natural join, a special case of an inner join. Specifying all.x = TRUE gives a left (outer) join, all.y = TRUE a right (outer) join, and both (all = TRUE) a (full) outer join. DBMSes do not match NULL records, equivalent to incomparables = NA in R.

See Also

data.frame, by, cbind.

dendrogram for a class which has a merge method.

Examples

Run this code

# NOT RUN {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 matchmerge(x, y, by = "k1") # NA's match, so 6 rowsmerge(x, y, by = "k2", incomparables = NA) # 2 rows# }

Run the code above in your browser using DataLab

Continue Improving Your R Skills

R Fundamentals

Level-up your R programming skills! Learn how to work with common data structures, optimize code, and write your own functions.

Big Data with R

Work with big data in R via parallel programming, interfacing with Spark, writing scalable & efficient R code, and learn ways to visualize big data.

Machine Learning with R

A machine learning scientist researches new approaches and builds machine learning models.
merge function - RDocumentation (2024)
Top Articles
Capitalize My Title
What is App Tracking Transparency (ATT)? | Adjust
Was ist ein Crawler? | Finde es jetzt raus! | OMT-Lexikon
Craglist Oc
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Obituaries
Parks in Wien gesperrt
7543460065
GAY (and stinky) DOGS [scat] by Entomb
Back to basics: Understanding the carburetor and fixing it yourself - Hagerty Media
Bed Bath And Body Works Hiring
Dark Souls 2 Soft Cap
Shariraye Update
Sport Clip Hours
Craigslist Pets Southern Md
Everything You Need to Know About Holly by Stephen King
Trini Sandwich Crossword Clue
Darksteel Plate Deepwoken
VMware’s Partner Connect Program: an evolution of opportunities
What is Rumba and How to Dance the Rumba Basic — Duet Dance Studio Chicago | Ballroom Dance in Chicago
Niche Crime Rate
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Tu Pulga Online Utah
Busted News Bowie County
Somewhere In Queens Showtimes Near The Maple Theater
Providence Medical Group-West Hills Primary Care
Menus - Sea Level Oyster Bar - NBPT
Bennington County Criminal Court Calendar
PCM.daily - Discussion Forum: Classique du Grand Duché
Naval Academy Baseball Roster
Elbert County Swap Shop
Snohomish Hairmasters
Black Panther 2 Showtimes Near Epic Theatres Of Palm Coast
Delta Township Bsa
Mynahealthcare Login
Nearest Ups Ground Drop Off
Www Mydocbill Rada
Lawrence Ks Police Scanner
Gridwords Factoring 1 Answers Pdf
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
Sports Clips Flowood Ms
Robot or human?
Car Crash On 5 Freeway Today
Cross-Border Share Swaps Made Easier Through Amendments to India’s Foreign Exchange Regulations - Transatlantic Law International
Build-A-Team: Putting together the best Cathedral basketball team
Viewfinder Mangabuddy
Craigslist Pa Altoona
Why I’m Joining Flipboard
Nsav Investorshub
The Quiet Girl Showtimes Near Landmark Plaza Frontenac
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5654

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.