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.
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
. IfTRUE
, duplicates and order incols
are preserved. IfFALSE
, duplicates are removed while preserving order of first appearance.- preserve_NULL
Logical, default
FALSE
. IfTRUE
, returnsNULL
whencols = NULL
. IfFALSE
, returnscharacter(0)
whencols = 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
df <- data.frame(a = 1, b = 2, c = 3)
# NULL input handling
get_colnames(df, NULL)
#> character(0)
get_colnames(df, NULL, preserve_NULL = TRUE)
#> NULL
# Numeric input
get_colnames(df, c(2, 2, 1))
#> [1] "b" "a"
get_colnames(df, c(2, 2, 1), preserve_duplicates = TRUE)
#> [1] "b" "b" "a"
# Character input
get_colnames(df, c("c", "a", "c"))
#> [1] "c" "a"
get_colnames(df, c("c", "a", "c"), preserve_duplicates = TRUE)
#> [1] "c" "a" "c"