dplyr::rename() / dplyr::relocate()
rename(.data, ...)
relocate(.data, ..., .before = NULL, .after = NULL) Returns:
tibble · Updated March 13, 2026 · Tidyverse dplyr data-wrangling tidyverse
The rename() function changes column names without changing their position, while relocate() reorders columns without changing their names. Both are part of dplyr and always return a new tibble.
Syntax
rename(.data, ...)
relocate(.data, ..., .before = NULL, .after = NULL)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
.data | tibble / data.frame | — | Input data frame or tibble |
... | name-value pairs | — | For rename(): new_name = old_name. For relocate(): columns to move |
.before | column position | NULL | Move selected columns before this column |
.after | column position | NULL | Move selected columns after this column |
Examples
Basic renaming
Rename columns using the new = old syntax:
library(dplyr)
df <- data.frame(
id = 1:3,
name = c("Alice", "Bob", "Carol"),
score = c(85, 92, 78)
)
# Rename 'score' to 'total_score'
rename(df, total_score = score)
# id name total_score
# 1 1 Alice 85
# 2 2 Bob 92
# 3 3 Carol 78
Multiple renames
Rename multiple columns at once:
df <- data.frame(x = 1:3, y = 4:6, z = 7:9)
rename(df, a = x, b = y, c = z)
# a b c
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
Using relocate()
Move columns to new positions:
df <- data.frame(
id = 1:3,
name = c("Alice", "Bob", "Carol"),
score = c(85, 92, 78)
)
# Move 'score' to the first position
relocate(df, score)
# score id name
# 1 85 1 Alice
# 2 92 2 Bob
# 3 78 3 Carol
# Move 'name' after 'id'
relocate(df, name, .after = id)
# id name score
# 1 1 Alice 85
# 2 2 Bob 92
# 3 3 Carol 78
Combining rename and relocate
Chain operations for complex transformations:
df <- data.frame(
id = 1:3,
name = c("Alice", "Bob", "Carol"),
score = c(85, 92, 78)
)
df %>%
rename(total_score = score) %>%
relocate(name, .before = id)
# name id total_score
# 1 Alice 1 85
# 2 Bob 2 92
# 3 Carol 3 78
Common Patterns
Rename with select syntax
Use rename_with() to rename using a function:
df <- data.frame(
X1 = 1:3,
X2 = 4:6,
Y1 = 7:9
)
# Convert column names to lowercase
rename_with(df, tolower)
# x1 x2 y1
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
# Rename using a function on column names
rename_with(df, ~ paste0(.x, "_new"))
# X1_new X2_new Y1_new
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
Using .before and .after with multiple columns
Move multiple columns together:
df <- data.frame(
a = 1:3, b = 2:4, c = 3:5, d = 4:6
)
# Move columns 'b' and 'c' to the front
relocate(df, b:c, .before = a)
# b c a d
# 1 2 3 1 4
# 2 3 4 2 5
# 3 4 5 3 6