Replacement for "rename" in dplyr
Asked Answered
C

7

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?

Cestoid answered 1/2, 2014 at 19:25 Comment(0)
L
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)
Locomotion answered 1/10, 2014 at 16:35 Comment(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.Galateah
@Galateah I've added the clarification. It should show up after review.Neysa
If you have issues, maybe specifiying the package explicitly helps dplyr::rename(iris, petal_length = Petal.Length).Neurocoele
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.Lavish
Is it possible to rename columns if current column name is not known? Like colnames(df) <- c("bla","blu").Dominate
You can use setNames() if you're replacing the column names wholesale: df %>% mutate(foo = 1 +2) %>% setNames(c("blah", "blu", "bar"))Lakshmi
To make this workable in a pipeline I added the name of package: ... %>% dplyr::rename(new_name = old_name) %>% ...Xenogenesis
A
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
Alkoran answered 2/2, 2014 at 1:7 Comment(10)
FYI changes is exported (or it should be)Mowbray
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.Cestoid
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?Cestoid
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.Alkoran
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.Cestoid
I see you updated the code--thanks! This now reflects dplyr's current behavior.Cestoid
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 :)Alkoran
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.Locomotion
@RomainFrancois, I too would like a way to say, "select all variables and by the way rename this column"!Indohittite
@RomainFrancois @Locomotion 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.Antigen
M
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.

Mandamandaean answered 1/2, 2014 at 22:27 Comment(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::mutateBumkin
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.Bumkin
I changed the loading order of dplyr and plyr, thanks.Mandamandaean
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!Cestoid
does rename work by reference like setnames from data.table packageMomently
B
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
Bumkin answered 1/2, 2014 at 20:2 Comment(4)
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)Alkoran
Thanks for the comment Romain. Is there a reason changes is not exported from dplyr? Seems quite a useful function.Bumkin
I guess hadley mostly sees it as a development tool for us.Alkoran
It definitely should be exported. I may have just forgottenMowbray
L
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
Linger answered 4/7, 2018 at 11:36 Comment(0)
E
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"
Enfield answered 15/12, 2018 at 21:25 Comment(0)
A
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
Asmodeus answered 8/2, 2021 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.