Learn R Programming

SSBtools (version 1.8.2)

get_colnames: Get column names from a data.frame, tibble, or data.table

Description

This helper function returns column names based on either column indices (numeric) or column names (character). It works consistently across data.frame, tibble, and data.table objects.

Usage

get_colnames(data, cols, preserve_duplicates = FALSE, preserve_NULL = FALSE)

Value

A character vector of column names, or NULL if cols = NULL

and preserve_NULL = TRUE.

Arguments

data

A data frame, tibble, or data.table.

cols

Column selection, either as numeric indices, character names, or NULL.

preserve_duplicates

Logical, default FALSE. If TRUE, duplicates and order in cols are preserved. If FALSE, duplicates are removed while preserving order of first appearance.

preserve_NULL

Logical, default FALSE. If TRUE, returns NULL when cols = NULL. If FALSE, returns character(0) when cols = NULL.

Details

By default, cols = NULL returns character(0), matching the behavior of names(data[1, NULL, drop = FALSE]). If preserve_NULL = TRUE, the function instead returns NULL.

Examples

Run this code
df <- data.frame(a = 1, b = 2, c = 3)

# NULL input handling
get_colnames(df, NULL)
get_colnames(df, NULL, preserve_NULL = TRUE)

# Numeric input
get_colnames(df, c(2, 2, 1))
get_colnames(df, c(2, 2, 1), preserve_duplicates = TRUE)

# Character input
get_colnames(df, c("c", "a", "c"))
get_colnames(df, c("c", "a", "c"), preserve_duplicates = TRUE)

Run the code above in your browser using DataLab