124

I like plyr's renaming function rename. I have recently started using dplyr, and was wondering if there is an easy way to rename variables using a function from dplyr, that is as easy to use as to plyr's rename?

0

7 Answers 7

152

dplyr version 0.3 added a new rename() function that works just like plyr::rename(), but with the old and new names switched:

df <- rename(df, new_name = old_name)
7
  • 7
    Could you explain the syntax? That's more important than the command. I'm using rename(TheDataFrame,OldVarName=NewVarName) but I get Error: Unknown variables: NewVarName. and I don't understand why.
    – s_a
    Commented Dec 5, 2014 at 16:06
  • 2
    @s_a I've added the clarification. It should show up after review.
    – Ryan
    Commented Dec 11, 2014 at 3:17
  • 4
    If you have issues, maybe specifiying the package explicitly helps dplyr::rename(iris, petal_length = Petal.Length).
    – Boern
    Commented Mar 21, 2016 at 9:36
  • 3
    Two quick observations: the above command has to be assigned to the dataframe to take effect iris <- dplyr::rename(iris, petal_length = Petal.Length) and rename() does not handle variable names with spaces, for example, dplyr::rename(iris, petal_length = "petal length") produces an error. Commented Mar 8, 2017 at 9:09
  • 2
    You can use setNames() if you're replacing the column names wholesale: df %>% mutate(foo = 1 +2) %>% setNames(c("blah", "blu", "bar"))
    – crazybilly
    Commented Jun 20, 2017 at 13:22
47

The next version of dplyr will support an improved version of select that also incorporates renaming:

> mtcars2 <- select( mtcars, disp2 = disp )
> head( mtcars2 )
                  disp2
Mazda RX4         160
Mazda RX4 Wag     160
Datsun 710        108
Hornet 4 Drive    258
Hornet Sportabout 360
Valiant           225
> changes( mtcars, mtcars2 )
Changed variables:
      old         new
disp  0x105500400
disp2             0x105500400

Changed attributes:
      old         new
names 0x106d2cf50 0x106d28a98
10
  • 2
    FYI changes is exported (or it should be)
    – hadley
    Commented Feb 3, 2014 at 15:35
  • 2
    Nice. Only thing is this will mean a shift in thinking on the user's part, since plyr's rename function uses "old"="new" whereas dplyr uses new=old which does keep it consistent with the rest of the dplyr functions. Personally, I don't think of it as a problem--you get used to new things quickly especially when it means a significant speedup in your data processing.
    – vergilcw
    Commented Feb 3, 2014 at 15:47
  • 3
    This is the intended feature, hence the choice of the verb select. Not sure we have something that says select all variables and by the way rename this column. Commented Feb 26, 2014 at 7:09
  • 1
    Perhaps to avoid confusion could you edit your post so that the code reflects the way select actually behaves? I would put in a vote for an easy dplyr way to keep all variables and just rename one or two. :) For now I'll keep loading plyr and using rename.
    – vergilcw
    Commented Feb 26, 2014 at 16:09
  • 2
    @RomainFrancois @aaronwolen You can achieve what the OP wants using mtcars %>% select(matches(".*"),disp2=disp). I would love a more parsimonious solution but this works and preserves all columns (though not their order). disp does not get duplicated.
    – farnsy
    Commented Aug 22, 2014 at 3:38
27

You can actually use plyr's rename function as part of dplyr chains. I think every function that a) takes a data.frame as the first argument and b) returns a data.frame works for chaining. Here is an example:

library('plyr')
library('dplyr')

DF = data.frame(var=1:5)

DF %>%
    # `rename` from `plyr`
    rename(c('var'='x')) %>%
    # `mutate` from `dplyr` (note order in which libraries are loaded)
    mutate(x.sq=x^2)

#   x x.sq
# 1 1    1
# 2 2    4
# 3 3    9
# 4 4   16
# 5 5   25

UPDATE: The current version of dplyr supports renaming directly as part of the select function (see Romain Francois post above). The general statement about using non-dplyr functions as part of dplyr chains is still valid though and rename is an interesting example.

5
  • 5
    It is best to load dplyr after plyr in this case. That way the faster dplyr functions are used when available and you can use mutate rather than dplyr::mutate
    – Vincent
    Commented Feb 1, 2014 at 22:43
  • Looks like you are right about being able to use non-dplyr functions in chaining. mtcars %.% rename(c("mpg","cyl"), c("mympg","mycyl")) works where rename is the function defined in my answer.
    – Vincent
    Commented Feb 1, 2014 at 23:10
  • I changed the loading order of dplyr and plyr, thanks. Commented Feb 1, 2014 at 23:30
  • This is a decent workaround, though brings up an interesting discussion about performance on larger data, which is one of the main advantages of dplyr. Thanks for the suggestion!
    – vergilcw
    Commented Feb 3, 2014 at 15:35
  • does rename work by reference like setnames from data.table package Commented Sep 4, 2015 at 0:11
9

It is not listed as a function in dplyr (yet): http://cran.rstudio.org/web/packages/dplyr/dplyr.pdf

The function below works (almost) the same if you don't want to load both plyr and dplyr

rename <- function(dat, oldnames, newnames) {
  datnames <- colnames(dat)
  datnames[which(datnames %in% oldnames)] <- newnames
  colnames(dat) <- datnames
  dat
}

dat <- rename(mtcars,c("mpg","cyl"), c("mympg","mycyl"))
head(dat)

                  mympg mycyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4          21.0     6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag      21.0     6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710         22.8     4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive     21.4     6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout  18.7     8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant            18.1     6  225 105 2.76 3.460 20.22  1  0    3    1

Edit: The comment by Romain produces the following (note that the changes function requires dplyr .1.1)

> dplyr:::changes(mtcars, dat)
Changed variables:
          old         new        
disp      0x108b4b0e0 0x108b4e370
hp        0x108b4b210 0x108b4e4a0
drat      0x108b4b340 0x108b4e5d0
wt        0x108b4b470 0x108b4e700
qsec      0x108b4b5a0 0x108b4e830
vs        0x108b4b6d0 0x108b4e960
am        0x108b4b800 0x108b4ea90
gear      0x108b4b930 0x108b4ebc0
carb      0x108b4ba60 0x108b4ecf0
mpg       0x1033ee7c0            
cyl       0x10331d3d0            
mympg                 0x108b4e110
mycyl                 0x108b4e240

Changed attributes:
          old         new        
names     0x10c100558 0x10c2ea3f0
row.names 0x108b4bb90 0x108b4ee20
class     0x103bd8988 0x103bd8f58
4
  • 3
    The only issue here is that data is copied. No big deal if this is for playing, i.e. mtcars etc ... but quite dramatic if you deal with substantial data. check dplyr:::changes(mtcars, dat) Commented Feb 2, 2014 at 0:10
  • 1
    Thanks for the comment Romain. Is there a reason changes is not exported from dplyr? Seems quite a useful function.
    – Vincent
    Commented Feb 2, 2014 at 7:20
  • 1
    I guess hadley mostly sees it as a development tool for us. Commented Feb 2, 2014 at 8:43
  • 1
    It definitely should be exported. I may have just forgotten
    – hadley
    Commented Feb 3, 2014 at 15:36
2

While not exactly renaming, dplyr::select_all() can be used to reformat column names. This example replaces spaces and periods with an underscore and converts everything to lower case:

iris %>%  
  select_all(~gsub("\\s+|\\.", "_", .)) %>% 
  select_all(tolower) %>% 
  head(2)
  sepal_length sepal_width petal_length petal_width species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
1

I tried to use dplyr::rename and I get an error:

occ_5d <- dplyr::rename(occ_5d, rowname='code_5d')
Error: Unknown column `code_5d` 
Call `rlang::last_error()` to see a backtrace

I instead used the base R function which turns out to be quite simple and effective:

names(occ_5d)[1] = "code_5d"
0

dplyr >= 1.0.0

In addition to dplyr::rename in newer versions of dplyr is rename_with()

rename_with() renames columns using a function.

You can apply a function over a tidy-select set of columns using the .cols argument:

iris %>% 
  dplyr::rename_with(.fn = ~ gsub("^S", "s", .), .cols = where(is.numeric))

    sepal.Length sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.