Renaming columns in a data frame
Problem
You want to rename the columns in a data frame.
Solution
Start with a sample data frame with three columns:
d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
d
#> alpha beta gamma
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
names(d)
#> [1] "alpha" "beta" "gamma"
The simplest way is to use rename()
from the plyr package:
library(plyr)
rename(d, c("beta"="two", "gamma"="three"))
#> alpha two three
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
If you don’t want to rely on plyr, you can do the following with R’s built-in functions. Note that these modify d
directly; that is, you don’t have to save the result back into d
.
# Rename column by name: change "beta" to "two"
names(d)[names(d)=="beta"] <- "two"
d
#> alpha two gamma
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
# You can also rename by position, but this is a bit dangerous if your data
# can change in the future. If there is a change in the number or positions of
# columns, then this can result in wrong data.
# Rename by index in names vector: change third item, "gamma", to "three"
names(d)[3] <- "three"
d
#> alpha two three
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
It’s also possible to use R’s string search-and-replace functions to rename columns. Note that the ^
and $
surrounding alpha
are there to ensure that the entire string matches. Without them, if there were a column named alphabet
, it would also match, and the replacement would be onebet
.
names(d) <- sub("^alpha$", "one", names(d))
d
#> one two three
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
# Across all columns, replace all instances of "t" with "X"
names(d) <- gsub("t", "X", names(d))
d
#> one Xwo Xhree
#> 1 1 4 7
#> 2 2 5 8
#> 3 3 6 9
# gsub() replaces all instances of the pattern in each column name.
# sub() replaces only the first instance in each column name.