Rename Columns in an R Data Frame
In R, you can rename data frame columns with base R functions such as colnames() and names(), or with dplyr::rename(). The appropriate method depends on whether you need to rename every column, one column by position, selected columns by their current names, or many columns using a naming rule.
Column names of an R Data frame can be accessed using the function colnames(). We can also access the individual column names using an index to the output of colnames() just like an array with notation colnames(df)[index].
To rename columns of an R Data Frame, assign colnames(dataframe) with the required vector of column names. To change a single column name, we may use index notation.
Base R Syntax for Renaming Data Frame Columns
The syntax to rename all the column of an R Data Frame df using colnames() is
colnames(df) <- new_names
where new_names is a vector of new column names.
The syntax to rename single column of an R Data Frame df using colnames() with index is
colnames(df)[index] <- new_name
where new_name is the new column name for column in position given by index.
The names() function can also be used with a data frame because a data frame is a named list of columns.
names(df) <- new_names
names(df)[index] <- new_name
Rename All Columns in an R Data Frame
In this example, we create an R data frame df and set the column names with the vector c("p", "q", "r").
example.R
df <- data.frame(a = c(41, 42, 43, 44),
b = c(45, 46, 47, 48),
c = c(49, 50, 51, 52))
print("Original Data Frame")
print(df)
colnames(df) <- c("p", "q", "r")
print("After changing column names")
print(df)
Output
[1] "Original Data Frame"
a b c
1 41 45 49
2 42 46 50
3 43 47 51
4 44 48 52
[1] "After changing column names"
p q r
1 41 45 49
2 42 46 50
3 43 47 51
4 44 48 52
The column names of the Data Frame changed to the new values.
When replacing every column name, the number of values in the new-name vector should match the number of columns in the data frame. You can check this with ncol(df).
if (length(c("p", "q", "r")) == ncol(df)) {
colnames(df) <- c("p", "q", "r")
}
Rename One R Data Frame Column by Index
Now, let us change the column name of column with index = 2 to "w".
example.R
df <- data.frame(a = c(41, 42, 43, 44),
b = c(45, 46, 47, 48),
c = c(49, 50, 51, 52))
print("Original Data Frame")
print(df)
colnames(df)[2] <- "w"
print("After changing column name")
print(df)
Output
[1] "Original Data Frame"
a b c
1 41 45 49
2 42 46 50
3 43 47 51
4 44 48 52
[1] "After changing column name"
a w c
1 41 45 49
2 42 46 50
3 43 47 51
4 44 48 52
R uses one-based indexing, so index 2 refers to the second column. Renaming by index is concise, but it can become unreliable if the order of columns changes later.
Rename a Column by Its Existing Name in Base R
When the current column name is known, you can locate it with a logical condition. This is usually clearer than relying on a fixed position.
df <- data.frame(
employee_id = c(101, 102, 103),
employee_name = c("Asha", "Ravi", "Meera")
)
colnames(df)[colnames(df) == "employee_name"] <- "name"
print(df)
Output
employee_id name
1 101 Asha
2 102 Ravi
3 103 Meera
If the old name does not exist, the expression makes no change. For scripts in which a missing column should be treated as an error, check the name first.
old_name <- "employee_name"
new_name <- "name"
if (!old_name %in% colnames(df)) {
stop("Column not found: ", old_name)
}
colnames(df)[colnames(df) == old_name] <- new_name
Rename Multiple Selected Columns in Base R
To rename a selected set of columns, store the old and new names in named vectors. The following approach avoids replacing names that are not present in the data frame.
df <- data.frame(
emp_id = c(1, 2),
emp_name = c("Asha", "Ravi"),
dept = c("Sales", "IT")
)
rename_map <- c(
emp_id = "employee_id",
emp_name = "employee_name"
)
matched <- match(colnames(df), names(rename_map))
replace <- !is.na(matched)
colnames(df)[replace] <- unname(rename_map[matched[replace]])
print(df)
Output
employee_id employee_name dept
1 1 Asha Sales
2 2 Ravi IT
Rename Columns with dplyr::rename()
The rename() function from dplyr is useful when working with tidyverse pipelines. Its naming order is new_name = old_name.
df <- dplyr::rename(df, new_name = old_name)
The following example renames two columns while leaving the remaining columns unchanged.
df <- data.frame(
emp_id = c(1, 2),
emp_name = c("Asha", "Ravi"),
dept = c("Sales", "IT")
)
df <- dplyr::rename(
df,
employee_id = emp_id,
employee_name = emp_name
)
print(df)
Output
employee_id employee_name dept
1 1 Asha Sales
2 2 Ravi IT
You may also use rename() inside a pipe.
df <- df |>
dplyr::rename(
employee_id = emp_id,
employee_name = emp_name
)
Rename Columns from a Character Vector with dplyr
When old and new column names are stored as character values, create a named vector and use the tidy-select splice operator. In the vector, the names are the new column names and the values are the existing column names.
rename_map <- c(
employee_id = "emp_id",
employee_name = "emp_name"
)
df <- dplyr::rename(df, !!!rename_map)
This form is useful when the mapping comes from configuration data or is constructed programmatically.
Rename All R Columns with a Prefix, Suffix, or Naming Rule
Use dplyr::rename_with() when the same transformation must be applied to several column names. For example, the following code adds the prefix raw_ to every column.
df <- data.frame(
id = c(1, 2),
score = c(86, 91)
)
df <- dplyr::rename_with(df, ~ paste0("raw_", .x))
print(df)
Output
raw_id raw_score
1 1 86
2 2 91
You can limit the transformation to selected columns. This example converts only columns ending in _value to uppercase.
df <- dplyr::rename_with(
df,
toupper,
dplyr::ends_with("_value")
)
Make R Column Names Unique and Syntactically Valid
R can store non-standard column names, including names with spaces, but such names may require backticks when referenced. The base R function make.names() converts names into syntactically valid identifiers, while unique = TRUE also resolves duplicates.
df <- data.frame(
"First Name" = c("Asha", "Ravi"),
"First Name" = c("Kumar", "Shah"),
check.names = FALSE
)
colnames(df) <- make.names(colnames(df), unique = TRUE)
print(colnames(df))
Output
[1] "First.Name" "First.Name.1"
For readable project conventions, many R users prefer lowercase names separated with underscores, such as employee_name and order_date.
Check Column Names Before and After Renaming
Use colnames() or names() to inspect the current names. After renaming, verify that the expected names exist and that no unintended duplicates were introduced.
print(colnames(df))
stopifnot("employee_id" %in% colnames(df))
stopifnot(!anyDuplicated(colnames(df)))
Remember that renaming a column changes only its label. It does not change the values, data type, or order of the column.
Common Errors When Renaming Columns in R
- Supplying the wrong number of names: when replacing all names, provide one new name for each column.
- Using zero-based indexing: R column positions begin at
1, not0. - Reversing dplyr rename arguments: use
new_name = old_namewithdplyr::rename(). - Renaming by position in changing data: prefer an existing-name match when input column order may vary.
- Creating duplicate names: check with
anyDuplicated(colnames(df))or repair names withmake.names(..., unique = TRUE).
Frequently Asked Questions About Renaming R Columns
How do I rename one column in an R data frame?
Use colnames(df)[index] <- "new_name" to rename by position, or use colnames(df)[colnames(df) == "old_name"] <- "new_name" to rename by the existing name.
How do I rename multiple columns in R?
In base R, assign selected entries of colnames(df) or apply a mapping vector. With dplyr, use dplyr::rename(df, new1 = old1, new2 = old2).
What is the difference between names() and colnames() for a data frame?
For a data frame, both functions return and set column names. names() reflects that a data frame is a named list, while colnames() is also used with matrix-like objects.
How do I add a prefix to every column name in R?
In base R, use colnames(df) <- paste0("prefix_", colnames(df)). With dplyr, use rename_with(df, ~ paste0("prefix_", .x)).
Does renaming a column change the data stored in it?
No. Renaming changes the column label only. The values, classes, and row order remain unchanged.
Editorial QA Checklist for R Column Renaming Examples
- Confirm that each replacement vector has the same length as the number of columns when all names are changed.
- Verify that examples using an index account for R’s one-based indexing.
- Check that every
dplyr::rename()example usesnew_name = old_name. - Run each example and compare the printed column names with the displayed output.
- Ensure that renamed columns are unique when later code depends on unambiguous column references.
Summary of R Data Frame Column Renaming Methods
Use colnames() or names() for direct base R assignments, colnames(df)[index] for a known position, a name-matching condition for a known existing label, and dplyr::rename() for explicit tidyverse renaming. Use rename_with() when several names follow the same transformation rule.
In this R Tutorial, we learned to rename columns of R Data Frame using colnames(dataframe), with the help of examples.
TutorialKart.com