123

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
vergilcw
  • 2,093
  • 4
  • 16
  • 20

7 Answers7

151

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)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
aaronwolen
  • 3,723
  • 1
  • 20
  • 21
  • 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 Dec 05 '14 at 16:06
  • 2
    @s_a I've added the clarification. It should show up after review. – Ryan Dec 11 '14 at 03:17
  • 4
    If you have issues, maybe specifiying the package explicitly helps `dplyr::rename(iris, petal_length = Petal.Length)`. – Boern Mar 21 '16 at 09: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. – Anthony Simon Mielniczuk Mar 08 '17 at 09:09
  • Is it possible to rename columns if current column name is not known? Like `colnames(df) <- c("bla","blu")`. – mindlessgreen Apr 25 '17 at 18:59
  • 2
    You can use `setNames()` if you're replacing the column names wholesale: `df %>% mutate(foo = 1 +2) %>% setNames(c("blah", "blu", "bar"))` – crazybilly Jun 20 '17 at 13:22
  • To make this workable in a pipeline I added the name of package: ... %>% dplyr::rename(new_name = old_name) %>% ... – Andrii Aug 07 '18 at 07:27
46

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
qwr
  • 9,525
  • 5
  • 58
  • 102
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
  • 2
    FYI `changes` is exported (or it should be) – hadley Feb 03 '14 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 Feb 03 '14 at 15:47
  • I just installed dplyr 0.1.2, and noticed what seems to be a bug. Running Romain's code above `mtcars2 <- select( mtcars, disp2 = disp )` returns a dataframe containing only the renamed variable, where it should return all variables, just with `disp` named `disp2`. Can anyone else confirm? – vergilcw Feb 25 '14 at 23:14
  • 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. – Romain Francois Feb 26 '14 at 07: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 Feb 26 '14 at 16:09
  • I see you updated the code--thanks! This now reflects dplyr's current behavior. – vergilcw Apr 07 '14 at 19:28
  • Sure. I had to do it. Someone else stepped in to do it, but this was refused by random so users who felt the change was too radical :) – Romain Francois Apr 07 '14 at 21:55
  • 1
    No doubt `select()` is an incredibly useful function but not really a replacement for `plyr::rename()`. I may be missing something but it seems like the answer to the original question is no. – aaronwolen Jun 19 '14 at 18:53
  • @RomainFrancois, I too would like a way to say, "select all variables and by the way rename this column"! – Statwonk Jul 08 '14 at 20:16
  • 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 Aug 22 '14 at 03: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.

user2503795
  • 4,035
  • 2
  • 34
  • 49
  • 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 Feb 01 '14 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 Feb 01 '14 at 23:10
  • I changed the loading order of dplyr and plyr, thanks. – user2503795 Feb 01 '14 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 Feb 03 '14 at 15:35
  • does rename work by reference like setnames from data.table package – MySchizoBuddy Sep 04 '15 at 00: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
Vincent
  • 5,063
  • 3
  • 28
  • 39
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
sbha
  • 9,802
  • 2
  • 74
  • 62
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"
Adams
  • 21
  • 1
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
LMc
  • 12,577
  • 3
  • 31
  • 43