absorb_dt: Absorb Columns from One data.table into Another

View source: R/dt_ops.R

absorb_dtR Documentation

Absorb Columns from One data.table into Another

Description

This function copies columns from a secondary data.table ('dt_i') into a primary data.table ('dt_x') based on matching key columns. If the join key is set to ".NATURAL" (the default), the function automatically determines the keys as the intersection of column names from 'dt_x' and 'dt_i', excluding any specified in 'exclude_col'. Any non-key columns in 'dt_x' that share names with columns in 'dt_i' are replaced by the corresponding columns from 'dt_i'. This is achieved by temporarily renaming the columns in 'dt_i', performing the join, and then restoring the original column names.

Usage

absorb_dt(dt_x, dt_i, on = ".NATURAL", exclude_col = NULL, verbose = FALSE)

Arguments

dt_x

A data.table that will have its columns updated.

dt_i

A data.table from which columns are to be copied.

on

A character vector specifying the join keys. If set to ".NATURAL", the keys are automatically determined as the common columns between 'dt_x' and 'dt_i', excluding any specified in 'exclude_col'.

exclude_col

An optional character vector of column names to exclude from the automatically determined join keys.

verbose

Logical. If TRUE, outputs messages indicating which columns in 'dt_x' have been replaced by columns from 'dt_i'.

Value

Returns 'dt_x' invisibly after modifying it in place by copying columns from 'dt_i'.

Examples

library(data.table)
dt_x <- data.table(a = 1:5, b = 1:5, c = 5:9, d = 5:1, e = 1:5)
dt_i <- data.table(a = 1:5, b = 1:5, c = 1:5, d = 1:5)
absorb_dt(dt_x, dt_i, on = c("a", "b"))

ChristK/CKutils documentation built on April 11, 2025, 10:11 p.m.