smerge: STATA Style Merge

Description Usage Details Value Author(s) Examples

Description

A wrapper function for the base function merge. Inherits all arguments from merge and provides an additional flag column labeled _merge. smerge also outputs counts of matched and unmatched observations from each data frame. Refer to merge for argument descriptions.

Usage

1
2
3
smerge <- function(x, y, by = intersect(names(x), names(y)),
                   by.x = by, by.y = by, all = TRUE, all.x = all, all.y = all,
                   sort = TRUE, suffixes = c(".x",".y"))

Details

Unlike merge, smerge defaults to all = TRUE. The numeric codes for the merge column are also different from those used in STATA. -1 and 1 denote unmatched observations from x and y respectively. 0 denotes a matched observation

Value

A data frame formed by merging x and y with an additional column labeled _merge

Author(s)

Sven Halvorson (svenedmail@gmail.com)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
test1 = data.frame(letter = c("A","A","B","C","D"),
                   num = 1:5,
                   pork = c("bacon","carnitas","carnitas","ham","chops"))



test2 = data.frame(letter = c("B","C","E","A"),
                   num = c(2,3,4,2),
                   chicken = c("wings","sammich","fried","drumstick"))


smerge(test1,test2)

svenhalvorson/SvenSFPS documentation built on May 21, 2019, 11:42 a.m.