stata_merge: Run merges like you would in Stata

Description Usage Arguments Examples

Description

This function merges dataframes the same way it's done in Stata. As in Stata, the user must define which of the dataframes is unique on the merge variables. The default function returns the merged dataframe with a variable that indicates which of the original dataframes each observation appears in.

Usage

1
2
stata_merge(master, using, by_vars, merge_type = "1:1", merge_var = "merge",
  gen = TRUE, keep_cases = c(1, 2, 3))

Arguments

master

the primary dataframe

using

the dataframe being merged into the master dataframe

by_vars

a vector of variables on which the merge is being conducted

merge_type

the type of Stata merge you want to run. Options that parse are "1:1", "m:1", and "1:m" where the choice for the master dataframe appears before the colon, the choice for the using dataframe appears after the colon, "1" means that dataframe is unique on the merging variables, and "m" means that dataframe is not unique on the merging variables.

merge_var

a name for the merge indicator that is added to the new dataframe. Defaults to "merge".

gen

controls whether new dataframe contains the merge indicator. Defaults to TRUE.

keep_cases

a vector that contains the types of observations to keep in the new dataframe. 1 captures those only in master, 2 captures those only in using, 3 captures those observations found in both dataframes.

Examples

1
2
3
data1 <- data.frame(a=1:10, b=rnorm())
data2 <- data.frame(a=1:10, c=rnorm())
stata_merge(data1, data2, "a")

danmckinleythompson/doStata documentation built on May 14, 2019, 4:02 a.m.