To learn how to rename a column in R, use names() or colnames() for direct assignment, or dplyr::rename() for name-based changes. Base R can target one position or replace the complete name vector, while dplyr uses the clearer new_name = old_name form.
The basic rename column in R pattern depends on whether you know the column’s position, its current name, or the complete set of names.
How to rename a column in R with names()
Create a data frame, then assign a new name to one element of its name vector:
R: df <- data.frame(id = 1:3, score = c(80, 90, 75), group = c(“A”, “B”, “A”))
R: names(df)[2] <- “points”
This changes the second column, score, to points. Position indexing starts at 1, so names(df)[1] refers to the first column.
You can also match the existing name instead of relying on its position:
R: names(df)[names(df) == “points”] <- “test_score”
Name matching is safer when column order might change. The expression finds every position whose current name equals points, then assigns test_score to those positions.
Replace multiple column names with names() or colnames()
To rename selected columns, provide matching position vectors on both sides:
R: names(df)[c(1, 3)] <- c(“student_id”, “cohort”)
The first replacement, student_id, goes to position 1. The second, cohort, goes to position 3. This is useful for selected-column replacement, but you must keep the position and replacement vectors in the same order and length.
For complete replacement, assign a vector containing one new name for every column:
R: names(df) <- c(“student_id”, “test_score”, “cohort”)
You can use colnames() the same way:
R: colnames(df) <- c(“student_id”, “test_score”, “cohort”)
Full replacement is position-based. A vector with too few or too many names can trigger an error or produce an unintended result, so count the columns before assigning the vector. Use length(names(df)) to confirm how many names you need.
Rename columns in R with dplyr::rename()
dplyr::rename() matches columns by their existing names, so you do not need to know their positions. Its ordering is new name first, followed by the old name:
R: df2 <- data.frame(first_name = c(“Ana”, “Luis”), score = c(88, 94), group = c(“A”, “B”))
R: df2 <- dplyr::rename(df2, points = score)
This changes score to points and leaves the other columns unchanged. Rename multiple columns by adding comma-separated pairs:
R: df2 <- dplyr::rename(df2, points = score, cohort = group)
Unlike full-list replacement with names(), dplyr does not require you to list unchanged columns. The data frame’s column order also stays the same; only the selected names change.
Check and troubleshoot the result
Inspect the names after any change:
R: names(df)
R: colnames(df)
If a base R name-matching assignment changes nothing, check that the old name is spelled exactly, including capitalization:
R: “test_score” %in% names(df)
A missing name in dplyr::rename() generally produces an error, while a condition that matches no names in base R may leave the data frame unchanged. For full replacement, verify both the count and order:
R: length(names(df))
R: anyDuplicated(names(df))
