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:

  1. d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
  2. d
  3. #> alpha beta gamma
  4. #> 1 1 4 7
  5. #> 2 2 5 8
  6. #> 3 3 6 9
  7. names(d)
  8. #> [1] "alpha" "beta" "gamma"

The simplest way is to use rename() from the plyr package:

  1. library(plyr)
  2. rename(d, c("beta"="two", "gamma"="three"))
  3. #> alpha two three
  4. #> 1 1 4 7
  5. #> 2 2 5 8
  6. #> 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.

  1. # Rename column by name: change "beta" to "two"
  2. names(d)[names(d)=="beta"] <- "two"
  3. d
  4. #> alpha two gamma
  5. #> 1 1 4 7
  6. #> 2 2 5 8
  7. #> 3 3 6 9
  8. # You can also rename by position, but this is a bit dangerous if your data
  9. # can change in the future. If there is a change in the number or positions of
  10. # columns, then this can result in wrong data.
  11. # Rename by index in names vector: change third item, "gamma", to "three"
  12. names(d)[3] <- "three"
  13. d
  14. #> alpha two three
  15. #> 1 1 4 7
  16. #> 2 2 5 8
  17. #> 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.

  1. names(d) <- sub("^alpha$", "one", names(d))
  2. d
  3. #> one two three
  4. #> 1 1 4 7
  5. #> 2 2 5 8
  6. #> 3 3 6 9
  7. # Across all columns, replace all instances of "t" with "X"
  8. names(d) <- gsub("t", "X", names(d))
  9. d
  10. #> one Xwo Xhree
  11. #> 1 1 4 7
  12. #> 2 2 5 8
  13. #> 3 3 6 9
  14. # gsub() replaces all instances of the pattern in each column name.
  15. # sub() replaces only the first instance in each column name.