mergedata: Merge data frames by key variables

View source: R/data-management.R

mergedataR Documentation

Merge data frames by key variables

Description

Merges a using data frame or file into a master data frame. Active data is used as master by default. Cardinality can be checked like Stata merges.

Usage

mergedata(
  using,
  by,
  data = NULL,
  type = c("1:1", "1:m", "m:1", "m:m"),
  join = c("full", "left", "right", "inner"),
  suffix = c("_master", "_using"),
  generate = "_merge",
  keep = NULL,
  update = FALSE,
  replace = FALSE,
  active = TRUE,
  quiet = FALSE,
  labels = c("factor", "labelled", "numeric")
)

Arguments

using

Data frame or existing file path.

by

Key variables: bare name, vars(...), or character vector.

data

Optional explicit master data frame.

type

One of "1:1", "1:m", "m:1", or "m:m".

join

One of "full", "left", "right", or "inner".

suffix

Two suffixes for overlapping non-key variables.

generate

Merge-status variable name, or NULL. Codes are 1 master only, 2 using only, and 3 matched.

keep

Optional status codes to retain, such as 3 or c(1, 3).

update

Logical; fill missing master values from using variables with the same names.

replace

Logical; with update = TRUE, replace nonmissing master values by nonmissing using values.

active

Logical; replace active data with the result.

quiet

Logical; suppress messages.

labels

Label handling when using is a file path.

Value

The merged data frame invisibly.

Examples

master <- data.frame(id = 1:3, age = c(20, 30, 40))
using <- data.frame(id = 2:4, sex = c("F", "M", "F"))
usedf(master, quiet = TRUE)
mergedata(using, by = id, type = "1:1", quiet = TRUE)
result <- usedf(quiet = TRUE)
result2 <- mergedata(using, data = master, by = id, type = "1:1",
                     active = FALSE, quiet = TRUE)

R4VN documentation built on Sept. 30, 2026, 5:13 p.m.