The “rename_all” Function in R
Package: dplyr
Purpose: To rename all columns in a data frame based on specified functions or patterns.
General Class: Data Manipulation
Required Argument(s):
data: The data frame to rename columns.
Notable Optional Arguments:
funs(): A list of functions or patterns to apply to column names.
Example (with Explanation):
# Load necessary packages
library(dplyr)
# Create a sample data frame
data <- data.frame(
ID = c(1, 2, 3),
Value_A = c(10, 15, 20),
Value_B = c(25, 30, 35)
)
# Rename all columns by adding "New_" prefix
renamed_data <- data %>%
rename_all(funs(paste("New_", ., sep = "")))
# Display the renamed data
print(renamed_data)In this example, the rename_all function from the dplyr package is used to rename all columns in the sample data frame data. The funs() argument specifies the renaming pattern, which adds a “New_” prefix to each column name. The result, renamed_data, contains the same data as data but with column names modified according to the specified pattern.