Learn R Programming

mStats (version 3.2.2)

leftJoin: Join two or more datasets

Description

leftJoin() merges two or multiple datasets sharing common variables and keeping all rows from x or master.

Usage

leftJoin(data, ..., by)

Arguments

data

master dataset

...

mergers or datasets to merge into master dataset

by

common variables

Value

Modified dataset in data.frame

Displaying notes

The notes are displayed in a fashion to inform the user what has been joined or not joined. This provides useful insights into one's own data. This is inspired by STATA.

Details

The join keeps all rows or observations in master dataset with matched observations from mergers. It adds one more variable merge_ to the resulting dataset. The value 1 of merge_ indicates the rows are from master dataset and for 2, rows from merger dataset and 3 is matched observations. In leftJoin, there can be both 1 or 3 in the return dataset.

Examples

Run this code
# NOT RUN {

## set seed
set.seed(123)
## first, create a patient dataset
patient <- data.frame(
    hospid = 1:100,
    docid = round(runif(100, 1, 15)),
    sex = runif(100, 1, 2),
    age = runif(100, 30, 60)
)

## now create a doctor dataset
doc <- data.frame(
   docid = c(1:10, 21:25),
   rating = round(runif(15, 1, 5))
)

## left join the two dataset
leftJoin(patient, doc, by = "docid")

## there are 36 records not matched, 31 not matched from master dataset,
## 5 not matched from merger dataset. 69 Final matched records


# }

Run the code above in your browser using DataLab