eventmerge: Merge Mobile EMA (mEMA) event-level data into momentary data

View source: R/eventmerge.R

eventmergeR Documentation

Merge Mobile EMA (mEMA) event-level data into momentary data

Description

This allows you to merge event-level data (e.g., yes/no to an event) into momentary data, placing each event on the most recent momentary datapoint at or before the event (for the same subject). The match is based on subject and timestamp, so it does not depend on the row order of either dataset or on how the mEMA KEY is constructed.

Usage

eventmerge(MOMENTARY, EVENT, eventNAME = "eventYN")

Arguments

MOMENTARY

a dataframe with momentary (i.e., level-1) data exported from mEMA. It must contain a "subject_id" column and a numeric "timestamp" column (other mEMA columns such as KEY and instance_key may be present and are passed through unchanged).

EVENT

a dataframe with event data (i.e., level-2). It must contain a "subject_id" column, a numeric "timestamp" column, and the event indicator in the LAST column (which can have any name). If a "local_date" column is present it is carried through as "date_event". Any other columns (e.g., respondent_id, survey_id, timezone_offset) are ignored.

eventNAME

variable name for your event in the final merged dataset (does not have to match last column in EVENT dataset, but can). Defaults to "eventYN".

Value

A dataframe equal to MOMENTARY (same rows, same order) with three columns added: the event indicator (named by eventNAME, 0 where no event maps to that momentary point), "timestamp_event" (the timestamp of the matched event, NA otherwise) and "date_event" (the event's local_date, NA otherwise). It has N rows = N rows in the momentary dataset. If more than one event maps to the same momentary datapoint, the most recent event is kept.

Examples

MOMENTARYdata <- data.frame(
  subject_id = c(1, 1, 1),
  timestamp  = c(100, 200, 300))
EVENTdata <- data.frame(
  subject_id = c(1, 1),
  timestamp  = c(150, 250),
  eventYN    = c(1, 1))
newDATA <- eventmerge(MOMENTARYdata, EVENTdata, eventNAME = "eventYN")

EMAtools documentation built on June 22, 2026, 5:08 p.m.