The rename function in R is commonly dplyr::rename(). It changes column labels without changing rows, values, or columns you do not select. For direct, readable mappings, use new_name = old_name. For full-vector control, use base R’s names().
The examples below assume an existing data frame named df. Both approaches can rename columns in R while preserving the data itself.
How does the rename function in R work?
dplyr::rename() returns a modified data frame and uses a deliberately explicit mapping:
- The new column name goes on the left.
- The existing column name goes on the right.
- Columns not listed in the call keep their names and positions.
Therefore, full_name = first_name changes first_name to full_name. Reversing the order produces the wrong result or an error if the proposed old name does not exist.
How do you rename columns in R with dplyr?
Use rename() for one column or several named changes. Refer to the package explicitly or load it with library(dplyr).
One column: df2 <- dplyr::rename(df, full_name = first_name)
This creates df2 with first_name renamed to full_name. The other columns remain unchanged. Add comma-separated mappings for multiple columns:
Several columns: df2 <- dplyr::rename(df, full_name = first_name, test_score = score)
Use rename_with() when a rule should transform selected names rather than mapping each name manually. This is useful for capitalization, prefixes, suffixes, or consistent formatting:
All names: df2 <- dplyr::rename_with(df, toupper)
Selected names: df2 <- dplyr::rename_with(df, ~ paste0(“score_”, .x), .cols = dplyr::starts_with(“score”))
The function receives the selected names as .x. In the second example, only names beginning with score receive the score_ prefix; other column names are untouched.
How can you rename in R with base R?
Base R stores a data frame’s column labels in its names vector. To change one column by its existing name, assign through a logical match:
One column: names(df)[names(df) == “first_name”] <- “full_name”
This method changes every matching name and leaves all other names intact. You can also rename by position, but position-based assignments are more fragile if the data-frame layout changes.
To replace the complete name vector, assign one new name for every column:
Full vector: names(df) <- c(“full_name”, “test_score”, “status”)
Full-vector assignment is appropriate when you know the exact column order. It changes every label, so it can accidentally rename columns you intended to preserve.
How do you check renamed columns and preserve the rest?
Inspect the resulting labels with names() immediately after either method:
names(df2)
For an automated check, verify the new name exists and the old name does not:
stopifnot(“full_name” %in% names(df2), !”first_name” %in% names(df2))
To confirm that untouched columns survived a dplyr rename, save the original names first:
old_names <- names(df)
df2 <- dplyr::rename(df, full_name = first_name)
stopifnot(all(setdiff(old_names, “first_name”) %in% names(df2)))
Use rename() for explicit old-to-new mappings, rename_with() for repeatable naming rules, and names() when you need direct control of one label or the entire name vector.



