Skip to main content

gather_column

Function gather_column 

Source
#[doc(hidden)]
pub unsafe fn gather_column(src: SEXP, idx: &[usize]) -> SEXP
Expand description

Gather the elements at idx (0-based, in order) out of a typed vector SEXP into a new dense SEXP of length idx.len(), where out[j] = src[idx[j]].

The selecting inverse of scatter_column: where scatter_column places a dense column’s values at sparse positions, gather_column pulls a dense subset out of a vector by element index. An element is a row only for a plain vector or list column: DataFrame::select_rows expands a row index to the element indices of every matrix column and recurses into data.frame columns before calling this.

Contiguous primitive columns (real/integer/logical/raw/complex) are copied as a slice gather via gather_native; string and list columns are copied element-by-element because they are write-barriered arrays of SEXP pointers, not flat buffers. The output type mirrors the input; any other type falls back to a logical NA column (normal data.frame columns never reach it).

Column attributes (class, levels, tzone, element names, …) are not copied — the caller restores them after rooting the gathered column (DataFrame::select_rows uses SexpExt::copy_most_attrib plus a names gather).

§Safety

Must be called on the R main thread. src must be a valid SEXP and every index in idx must be < xlength(src). The returned SEXP is unprotected; the caller must root it (e.g. via SET_VECTOR_ELT into a protected list) before performing any allocation.