View source: R/frenamecolumns.R
f_rename_columns | R Documentation |
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.
f_rename_columns(df, name_map)
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 |
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 in df
.
If these conditions are not met, the function will throw an error with an appropriate message.
A data frame with updated column names. Columns not specified in name_map
remain unchanged.
Sander H. van Delden plantmind@proton.me
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.