match.merge: Match and merge two data.frames by ID and date

View source: R/datawrangler.R

match.mergeR Documentation

Match and merge two data.frames by ID and date

Description

the rows from y are matched with rows from x that share the same ID and directly precede them in date.

Usage

match.merge(x, y, idvar, datevar, keep.unmatched = c("all", "x", "y", "none"))

Arguments

x, y

The data.frames to merge.

idvar

Name of the variable holding participant/session IDs in both x and y.

datevar

Name of the variable holding dates in both x and y.

keep.unmatched

When no match is found for a row, should it be kept or removed?

Details

This is useful for matching participant data from different datasets, where one dataset is always collected after another. It is assumed here that x is always administered before y.

This matches using [match.pps()] and merges using [dplyr::full_join()].

Value

A merged data.frame where rows of x are merged with rows of y that match by session id and where the date of each row from x directly precedes the date of the matched row from y.

Author(s)

Sercan Kahveci

Examples

x <- data.frame(id=c(1,2,2,3,3,4),
                date=as.POSIXct(c(1,1,3,1,3,1)),
               info=letters[1:6])
y <- data.frame(id=c(1,2,3,3,4),
               date=as.POSIXct(c(2,2,4,2,0)),
                data=LETTERS[1:5])
match.merge(x=x,y=y,idvar="id",datevar="date",keep.unmatched="all")
match.merge(x=x,y=y,idvar="id",datevar="date",keep.unmatched="none")


Spiritspeak/skMisc documentation built on April 12, 2025, 5:40 a.m.