f_rename_columns {rfriend} | R Documentation |
Rename Specific Columns in a Data Frame
Description
Renames specific columns in a data frame based on a named vector (name_map). It ensures that only the specified columns are renamed, while others remain unchanged.
Usage
f_rename_columns(df, name_map)
Arguments
df |
A data frame whose columns are to be renamed. |
name_map |
A named vector where the names correspond to the current column names in |
Details
This function is particularly useful when you want to rename only a subset of columns in a data frame. It performs input validation to ensure that:
-
name_map
is a named vector. All names in
name_map
exist as column names indf
.
If these conditions are not met, the function will throw an error with an appropriate message.
Value
A data frame with updated column names. Columns not specified in name_map
remain unchanged.
Author(s)
Sander H. van Delden plantmind@proton.me
Examples
# Create a sample data frame.
df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
# Define a named vector for renaming specific columns.
name_map <- c(a = "alpha", c = "gamma")
# Rename columns.
df <- f_rename_columns(df, name_map)
# View updated data frame.
print(df)