Skip to main content
v2026.11,986 entries · CC-BY 4.0

Coalesce in R: Syntax, Vector Priority, and Data Cleaning Workflows

Master coalesce in R using dplyr and data.table to resolve missing NA values across cascading vectors and data frame columns with strict type safety.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

In R programming, the coalesce() function finds the first non-missing (NA) value at each position across a sequence of vectors or data frame columns. Originating in SQL and implemented natively in the dplyr package (and as fcoalesce() in data.table), coalescing evaluates candidate values from left to right, returning a single consolidated vector that preserves attributes and enforces strict type compatibility across research datasets.

What it is used for in research

Empirical research projects frequently gather data from distributed, heterogeneous sources where missingness does not occur uniformly. In longitudinal cohort studies, clinical registries, and multi-wave social science surveys, participant observations may be split across multiple follow-up instruments, administrative claims, and proxy informant interviews. For example, a subject’s current contact phone number or primary laboratory measurement might be missing in the primary survey wave but recorded during an auxiliary clinical visit or a subsequent follow-up wave. A naive merge often leaves investigators with competing columns—such as baseline_score, midline_score, and proxy_score—that must be synthesized into a single, authoritative analytical variable without introducing systematic bias or silent data truncation.

This is where coalescing serves as a fundamental data hygiene primitive. Unlike generic imputation routines that estimate unobserved parameters through statistical models, coalescing implements a deterministic hierarchy of fallback values. Researchers establish an explicit priority order based on measurement validity or protocol chronology. The primary column is evaluated first; if an observation contains an NA, the algorithm tests the secondary column, continuing across subsequent candidate vectors until a valid observation is located or all options are exhausted. Because the evaluation sequence is explicitly defined in script syntax, coalescing ensures complete reproducibility, an essential requirement under FAIR data principles and institutional data governance frameworks.

Beyond cascading multi-column merges, coalescing is heavily utilized in experimental workflows to replace isolated missing values with standard default baselines. In epidemiological dosage monitoring and event-count models, unrecorded counts frequently represent zero occurrences rather than missing status, provided the protocol verifies active monitoring. Using coalesce() alongside standard verification workflows allows analysts to specify clean numeric or categorical fallbacks without triggering the class-stripping bugs common to legacy base R routines. Understanding the distinction between variable classifications—detailed in our reference on types of variables—helps researchers avoid conflating missing values with true negative baseline observations.

Coalescing also plays a central role in reconciling left joins and full outer joins. When two research tables are joined on participant identifiers, unmatched rows from the secondary table generate NA entries in the merged attributes. Analysts frequently use coalesce() inside dplyr::mutate() to collapse overlapping attributes from both tables into a single unified field, removing duplicate column clutter before executing statistical modeling or data archiving.

How to install or access it

The standard modern implementation of coalesce() is part of the dplyr package, which is hosted on the Comprehensive R Archive Network (CRAN). As of CRAN release 1.2.1, dplyr is maintained by Hadley Wickham under the MIT license and requires R version 4.1.0 or higher. For high-performance computing on large genomic or administrative records, the data.table package provides fcoalesce(), also hosted on CRAN (release 1.18.6.1, licensed under GPL-2 or higher) and requiring R version 3.4.0 or higher.

To install either package, execute the standard installation commands directly in your R console using an official CRAN mirror. Do not download compiled binaries from third-party distribution portals or unverified repositories:

# Install dplyr from CRAN
install.packages("dplyr")

# Install data.table from CRAN for high-performance vector coalescing
install.packages("data.table")

Once installed, attach the libraries to your active session. If you work within an enterprise or university compute cluster where installation permissions are managed by system administrators, check whether the packages are already accessible through installed.packages():

# Load the libraries into your active R environment
library(dplyr)
library(data.table)

If you prefer not to attach the entire namespace to prevent masking base R functions, access the functions directly using explicit package prefixes: dplyr::coalesce() and data.table::fcoalesce(). Base R also provides built-in mechanisms for missing-value substitution through logical vector indexing and is.na(), which require no external package installations.

Step-by-step task

Executing a robust coalescing workflow involves defining your data hierarchy, verifying vector types, and executing the operation using the appropriate function for your dataset size. Follow these numbered steps to implement coalescing in R.

Step 1: Inspect data types and verify vector compatibility

Before coalescing vectors or data frame columns, verify that all candidate inputs share a compatible data type. The dplyr::coalesce() function uses the vctrs type system. It does not perform loose automatic type conversions across incompatible types (such as character and numeric). You can explore how R manages atomic storage modes in our dedicated guide to R programming data types. Use class() or typeof() to inspect your candidate columns:

# Check storage classes of candidate vectors
primary_obs <- c(10.5, NA, 12.3, NA, 15.0)
backup_obs  <- c(9.8, 11.2, NA, 14.1, NA)

typeof(primary_obs)
# [1] "double"
typeof(backup_obs)
# [1] "double"

Step 2: Coalesce isolated vectors with a static fallback value

To replace NA values in a single vector with a default constant (such as zero or a designated baseline threshold), pass the vector as the first argument and the constant as the second argument:

library(dplyr)

# Replace missing values with a numeric constant
readings <- c(42, NA, 18, NA, 95)
cleaned_readings <- coalesce(readings, 0)
print(cleaned_readings)
# [1] 42  0 18  0 95

Note that if your primary vector is an integer (e.g., c(1L, NA, 3L)), passing a floating-point double (e.g., 0) will cast the integer vector to double. To preserve 32-bit integer storage mode, supply an integer literal fallback using the L suffix: coalesce(readings_int, 0L). When your analytical goal is strictly limited to zero substitution rather than multi-vector evaluation, compare this approach with the specialized methods outlined in replace NA with 0 in R.

Step 3: Cascade across multiple candidate vectors

When multiple vectors contain complementary observations, list them in order of decreasing reliability. The coalesce() function scans left-to-right at each index and extracts the first non-missing value:

# Define three prioritized measurement sources
sensor_primary   <- c(24.1, NA,   NA,   22.8, NA)
sensor_secondary <- c(NA,   23.5, 25.0, NA,   NA)
manual_audit     <- c(24.0, 23.4, NA,   22.9, 21.5)

# Cascade across all three sources with an absolute fallback
final_temp <- coalesce(sensor_primary, sensor_secondary, manual_audit)
print(final_temp)
# [1] 24.1 23.5 25.0 22.8 21.5

In this evaluation, index 1 takes sensor_primary (24.1); index 2 encounters NA in primary and takes sensor_secondary (23.5); index 3 encounters NA in primary and takes sensor_secondary (25.0); index 4 takes primary (22.8); and index 5 encounters NA in both sensors to retrieve manual_audit (21.5).

Step 4: Integrate coalesce within a dplyr data frame pipeline

In typical research workflows, your variables reside inside a tibble or data frame. Use coalesce() inside dplyr::mutate() to create a unified analytical column or overwrite an existing variable:

# Create a sample clinical cohort dataset
cohort <- tibble(
  patient_id   = 101:105,
  clinic_bp    = c(120, NA, 135, NA, 118),
  home_bp      = c(118, 128, NA, 142, NA),
  kiosk_bp     = c(NA, 126, 132, 140, 115)
)

# Synthesize blood pressure measurements according to clinical protocol
cohort_cleaned %
  mutate(
    resolved_bp = coalesce(clinic_bp, home_bp, kiosk_bp)
  )

print(cohort_cleaned)
# # A tibble: 5 x 5
#   patient_id clinic_bp home_bp kiosk_bp resolved_bp
#        [int]     [dbl]   [dbl]    [dbl]       [dbl]
# 1        101       120     118       NA         120
# 2        102        NA     128      126         128
# 3        103       135      NA      132         135
# 4        104        NA     142      140         142
# 5        105       118      NA      115         118

To deepen your understanding of chaining data transformations across research cohorts, consult our comprehensive overview of the dplyr package in R.

Step 5: Dynamic column selection with tidyselect and dynamic dots

When you must coalesce a large or variable number of columns matching a common naming convention (such as longitudinal survey waves wave_01, wave_02, …, wave_10), manual typing becomes error-prone. Modern dplyr (version 1.1.0 and later) allows you to select columns dynamically using pick() combined with the rlang dynamic dots splice operator (!!!):

survey_data <- tibble(
  subject_id = 1:3,
  score_w1   = c(85, NA, NA),
  score_w2   = c(82, 90, NA),
  score_w3   = c(NA, 88, 79)
)

# Coalesce across all columns starting with 'score_'
survey_resolved %
  mutate(
    final_score = coalesce(!!!pick(starts_with("score_")))
  )

print(survey_resolved)
# # A tibble: 3 x 5
#   subject_id score_w1 score_w2 score_w3 final_score
#        [int]    [dbl]    [dbl]    [dbl]       [dbl]
# 1          1       85       82       NA          85
# 2          2       NA       90       88          90
# 3          3       NA       NA       79          79

Step 6: High-performance coalescing with data.table

For datasets containing millions of rows, data.table::fcoalesce() provides optimized C-level speed and multithreading. It also accepts a list of vectors directly, eliminating the need for dynamic dots unpacking:

library(data.table)

# Convert cohort to a data.table
dt_cohort <- as.data.table(cohort)

# In-place column creation using fcoalesce
dt_cohort[, resolved_bp := fcoalesce(clinic_bp, home_bp, kiosk_bp)]

# Passing a list of column vectors directly
cols_to_check <- list(dt_cohort$clinic_bp, dt_cohort$home_bp, dt_cohort$kiosk_bp)
dt_cohort[, resolved_bp_fast := fcoalesce(cols_to_check)]

In addition, fcoalesce() includes a dedicated nan parameter. By default (nan = NA), IEEE NaN values are treated as missing and overwritten. If your analytical protocol requires distinguishing numeric computation errors (NaN) from uncollected observations (NA), pass nan = NaN to preserve NaN values during evaluation.

Output and how to read it

The output returned by coalesce() or fcoalesce() is an atomic vector or classed object matching the length of the longest input vector (or common size, in vctrs terminology) and the common data type of the supplied inputs. Understanding the structure and metadata of this output is essential for data validation.

When examining coalesced output, verify the following three properties:

  • Completeness and Residual Missingness: If every candidate vector contains an NA at index i, and no constant fallback was supplied, the output at position i will remain NA. You can audit residual missingness using sum(is.na(output)) or mean(is.na(output)) * 100 to determine the exact percentage of observations that could not be resolved from secondary or tertiary sources.
  • Preservation of S3 Attributes: Unlike base R’s ifelse(), which strips S3 attributes and reduces Date or POSIXct objects to raw numeric timestamps, dplyr::coalesce() and data.table::fcoalesce() preserve class attributes. A coalesced Date vector retains its Date class and calendar formatting:
    d1 <- as.Date(c("2026-01-15", NA, "2026-03-20"))
    d2 <- as.Date(c("2026-01-10", "2026-02-18", "2026-03-25"))
    
    coalesce(d1, d2)
    # [1] "2026-01-15" "2026-02-18" "2026-03-20"
  • Common Type Promotion: When coalescing compatible numeric types—such as integers and doubles—the output is promoted to the richer type (double) without warnings:
    int_vec <- c(1L, NA, 3L)
    num_vec <- c(NA, 2.5, 3.0)
    
    res <- coalesce(int_vec, num_vec)
    typeof(res)
    # [1] "double"
    print(res)
    # [1] 1.0 2.5 3.0

To inspect the full data frame output following a coalescing operation, use glimpse() or summary() to verify that the newly generated column displays the expected distribution and zero unintended missing values.

Troubleshooting

When working with coalesce() in complex analytical pipelines, investigators frequently encounter errors related to type strictness, factor level mismatches, and recycling dimensions. The solutions below resolve the most common issues.

Error: Can’t combine ..1 and ..2 with incompatible types

The most frequent error in modern dplyr::coalesce() occurs when candidate vectors possess incompatible data types:

v1 <- c("Case", NA, "Control")
v2 <- c(101, 102, 103)

coalesce(v1, v2)
# Error in `coalesce()`:
# ! Can't combine `..1` [character] and `..2` [double].

Cause: The vctrs engine does not automatically convert numbers to characters or characters to numbers during coalescing, as silent type coercion can conceal upstream data import bugs.

Fix: Cast candidate vectors to a shared type before calling coalesce(), or explicitly specify the desired target prototype using the .ptype argument:

# Cast explicitly before coalescing
coalesce(v1, as.character(v2))
# [1] "Case" "102"  "Control"

# Or declare output prototype via .ptype
coalesce(as.character(v1), as.character(v2), .ptype = character())

Error: Incompatible factor levels

In both dplyr::coalesce() and data.table::fcoalesce(), combining factors with different levels can trigger errors or produce corrupted factor indices:

f1 <- factor(c("Mild", NA, "Severe"), levels = c("Mild", "Moderate", "Severe"))
f2 <- factor(c("Low", "High", "Low"), levels = c("Low", "High"))

coalesce(f1, f2)
# Error in `coalesce()`:
# ! Can't combine `..1` and `..2` with differing factor levels.

Cause: R factors represent integer codes mapped to a character level attribute. Merging two factors that do not share an identical level set causes undefined index collisions.

Fix: Convert factors to characters prior to coalescing, or harmonize factor levels across all candidate vectors using forcats::fct_unify() before invoking coalesce():

# Convert to character during coalescing and recast to factor
factor(coalesce(as.character(f1), as.character(f2)))

Error: Input vectors have incompatible sizes

If you pass vectors of differing lengths that cannot be recycled (for instance, a vector of length 5 and a vector of length 3), coalesce() throws an incompatible size error:

coalesce(c(1, 2, NA, 4, 5), c(10, 20, 30))
# Error in `coalesce()`:
# ! Can't recycle `..1` (size 5) to match `..2` (size 3).

Cause: Under vctrs recycling rules, inputs must either have identical lengths or a length of 1 (a scalar that recycles across the entire vector).

Fix: Verify that all candidate vectors match the length of the data frame or cohort, or supply only scalar fallback values.

Unexpected Attribute Loss when using ifelse()

Researchers transitioning from legacy R scripts often attempt to replicate coalescing using ifelse(is.na(x), y, x). However, when x and y are Date objects, ifelse() strips the class attribute, returning raw numeric epoch days:

dates_x <- as.Date(c("2026-05-01", NA))
dates_y <- as.Date(c("2026-05-01", "2026-05-10"))

ifelse(is.na(dates_x), dates_y, dates_x)
# [1] 20574 20583  # Date class destroyed

Fix: Always use dplyr::coalesce(), data.table::fcoalesce(), or the conditional branching function detailed in our guide to case_when in R, all of which maintain full S3 class integrity.

Free alternatives

While dplyr::coalesce() is the most widely adopted function in the R tidyverse ecosystem, several open-source and base R alternatives provide specialized capabilities depending on your performance constraints, dependency tolerance, and data structures. The data table below compares the primary free alternatives available to R users.

Approach / Package Dependency Required Execution Engine Primary Advantage Key Limitation
dplyr::coalesce() dplyr, vctrs, rlang C / C++ (via vctrs) Strict type safety; seamless tidyverse and mutate integration Requires dynamic dots (!!!) when passing vector lists
data.table::fcoalesce() data.table Optimized C (multithreaded) Extreme speed on large datasets; accepts list inputs directly; nan parameter Requires identical factor levels across all candidate vectors
tidyr::replace_na() tidyr R / vctrs Clean syntax for replacing NAs with a single static value across columns Cannot cascade across multiple candidate vectors; replaces single values only
Base R Indexing (is.na) None (base R) Native C primitives Zero external dependencies; preserves S3 class attributes Cumbersome syntax for cascading across three or more vectors
Base R Functional Fold (Reduce) None (base R) Native R functional Zero dependencies; cascades cleanly across an arbitrary list of vectors Slower execution speed on high-dimensional data frames

When zero external package dependencies are required (for example, in base R package development or standalone CRAN submission scripts), you can implement a robust, class-preserving vector coalescing fold using base R’s Reduce() primitive:

# Base R zero-dependency coalescing function
base_coalesce <- function(...) {
  vectors <- list(...)
  Reduce(function(a, b) {
    replace_idx <- which(is.na(a))
    a[replace_idx] <- b[replace_idx]
    a
  }, vectors)
}

# Test with Date vectors
d_base1 <- as.Date(c("2026-04-01", NA, "2026-06-01"))
d_base2 <- as.Date(c(NA, "2026-05-15", "2026-06-15"))
base_coalesce(d_base1, d_base2)
# [1] "2026-04-01" "2026-05-15" "2026-06-01"

This functional approach avoids the attribute-stripping flaws of ifelse() while maintaining full dependency independence.

Frequently asked questions

What does coalesce do in R?

The coalesce() function evaluates one or more vectors or data frame columns position by position from left to right, returning the first non-missing (non-NA) value found at each index. It is widely used to combine multiple fallback columns or replace missing observations with a default value while preserving vector data types.

How does coalesce differ between dplyr and data.table?

While both functions follow SQL-style coalescing logic, dplyr::coalesce() is tightly integrated into the tidyverse, uses vctrs for strict type checking, and requires dynamic dots (!!!) to unpack lists of columns. In contrast, data.table::fcoalesce() is written in multithreaded C for higher performance, accepts lists directly, and provides a nan parameter to distinguish between NaN and NA.

Can coalesce handle different data types in R?

No, dplyr::coalesce() enforces strict type compatibility and will throw an error if you attempt to combine incompatible types, such as characters and integers. You must explicitly convert the vectors to a common type beforehand or supply a prototype via the .ptype argument. Compatible numeric types (integer and double) are automatically promoted to double.

Why does ifelse() strip Date classes while coalesce() does not?

Base R’s ifelse() is designed for simple atomic vectors and does not preserve S3 class attributes, causing Date, POSIXct, and factor vectors to revert to raw numeric codes. In contrast, coalesce() in dplyr and fcoalesce() in data.table are specifically engineered to preserve class attributes and metadata.

Sources

Data: Missing Value Coalescing and Replacement Functions in R

Comparative specifications, input signatures, type-coercion behaviors, and performance characteristics of missing-value coalescing and imputation functions across R packages, compiled from official CRAN and project documentation.

Period covered: 2026. Coverage: Global.

Missing Value Coalescing and Replacement Functions in R
Function NamePackageCRAN ReleaseEvaluation OrderType HandlingNaN TreatmentDirect List Input
dplyr::coalescedplyr1.2.1Left-to-right priorityvctrs common type (strict error on mismatch)Treated as NA (replaced)Requires dynamic dots splice (!!!)
data.table::fcoalescedata.table1.18.6.1Left-to-right priorityStrict class match (factors require identical levels)Configurable via nan argument (default NA)Supported directly (list or data.table)
tidyr::replace_natidyr1.3.1Target replacementMatches replacement value to vector typeTreated as value unless specifiedRequires named list for data frames
base::ifelsebaseR (>= 4.1.0)Element-wise conditionalCoerces to common mode (strips S3 class/Date)Preserved as condition test resultNot supported (vectors only)
base vector indexingbaseR (>= 4.1.0)Logical subscript replacementPreserves target vector class attributesIdentified by is.na() as TRUENot supported (requires iteration)

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Coalesce in R: Syntax, Vector Priority, and Data Cleaning Workflows

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →