Combine data
Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.
append
Stack two datasets
Stata
append using data2R Package:dplyr
bind_rows(data1, data2)Differs from Stata bind_rows() has R type-promotion and attribute rules; .id creates string source identifiers rather than the numeric codes of append, generate().
1 more examples and alternatives
Stack datasets and record where each row came from
Stata
append using data2, generate(source)R Package:dplyr
dplyr::bind_rows(data1, data2, .id = "source") |> dplyr::mutate(source = as.integer(source) - 1L)Differs from Stata Uses numeric source IDs: zero for the master and one for the first using dataset, as Stata append, generate() does. bind_rows() has R type-promotion and attribute rules; .id creates string source identifiers rather than the numeric codes of append, generate().
cross
Every combination of rows from two datasets
Stata
cross using data2R Package:dplyr
cross_join(data1, data2)Differs from Stata cross_join() suffixes overlapping column names; Stata cross keeps master values for overlapping names. Rename overlaps first to obtain the same columns.
joinby
All pairs of rows within each id
Stata
joinby id using data2R Package:dplyr
inner_join(data1, data2, by = "id", relationship = "many-to-many")Differs from Stata dplyr retains overlapping non-key columns with suffixes instead of Stata master values, creates no _merge, and does not enforce a declared 1:1 key constraint.
merge
Put two datasets side by side by row position
Stata
merge 1:1 _n using data2R Package:dplyr
bind_cols(data1, data2)Differs from Stata bind_cols() needs matching row counts (or recyclable one-row inputs) and repairs duplicate names; merge 1:1 _n can retain unmatched rows.
Merge with a match report
Stata
merge 1:1 id using data2R Package:tidystata
stata_merge(data1, data2, "1:1", by = "id")Like Stata, all rows are kept and a .merge column records the match result. full_join(data1, data2, by = “id”) merges without the report.
One-to-many merge with a match report
Stata
merge 1:m id using data2R Package:tidystata
stata_merge(data1, data2, "1:m", by = "id")Many-to-one merge with a match report
Stata
merge m:1 id using data2R Package:tidystata
stata_merge(data1, data2, "m:1", by = "id")6 more examples and alternatives
Keep all rows of the master data
Stata
merge 1:1 id using data2, keep(master match)R Package:dplyr
left_join(data1, data2, by = "id")Differs from Stata dplyr retains overlapping non-key columns with suffixes instead of Stata master values, creates no _merge, and does not enforce a declared 1:1 key constraint.
Keep rows of data1 whose id is not in data2
Stata
merge 1:1 id using data2, keep(master) keepusing(id) nogenerateR Package:dplyr
anti_join(data1, data2, by = "id")Differs from Stata semi_join()/anti_join() select rows by key membership but do not enforce the unique keys required by Stata merge 1:1; validate uniqueness separately.
Keep matched rows only
Stata
merge 1:1 id using data2, keep(match)R Package:dplyr
inner_join(data1, data2, by = "id")Differs from Stata dplyr retains overlapping non-key columns with suffixes instead of Stata master values, creates no _merge, and does not enforce a declared 1:1 key constraint.
Keep rows of data1 whose id is in data2
Stata
merge 1:1 id using data2, keep(match) keepusing(id) nogenerateR Package:dplyr
semi_join(data1, data2, by = "id")Differs from Stata semi_join()/anti_join() select rows by key membership but do not enforce the unique keys required by Stata merge 1:1; validate uniqueness separately.
Keep all rows of the using data
Stata
merge 1:1 id using data2, keep(using match)R Package:dplyr
right_join(data1, data2, by = "id")Differs from Stata dplyr retains overlapping non-key columns with suffixes instead of Stata master values, creates no _merge, and does not enforce a declared 1:1 key constraint.
Merge on two keys
Stata
merge 1:1 id1 id2 using data2, nogenerateR Package:dplyr
full_join(data1, data2, by = c("id1", "id2"))Differs from Stata Variables with the same name in both tables get suffixes; choose them with suffix = c(“_data1”, “_data2”). dplyr retains overlapping non-key columns with suffixes instead of Stata master values, creates no _merge, and does not enforce a declared 1:1 key constraint.