Error message when running simple 'rename' function in R

Mike picture Mike · May 31, 2015 · Viewed 60.4k times · Source

Below a very simple data frame example I found in the internet. Running this in RStudio on my machine turns out an error message:

Error: All arguments to rename must be named.

The rename function seems to be straight forward but doesn't work for some reasons and I can't figure out why.

library("dplyr")

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

rename(d, c("beta"="two", "gamma"="three"))

#Error: All arguments to rename must be named.

Answer

Valentin picture Valentin · Aug 5, 2015

Short answer

Mike, your command is valid but for "plyr" package. If you load the "dplyr" in the same script you will get the error that you mentioned.

Consequently, try this instead:

library("plyr")
d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
d <- plyr::rename(d, c("beta"="two", "gamma"="three"))

Some extra thoughts to better understand the issue

1) search()

Can use the function search() to find out the order in which R searches for functions/objects.

In the example below, besides the warnings that you get when loading two packages with identical function names, you can call search() to realize that R will look for functions first in ".GlobalEnv" (the default environment when you start R), then in "package:dplyr" and then in "package:plyr" and so on. So you get the error message because R thinks you want to use the rename() function from the dplyr package (which has precedence over plyr because is more recently loaded).

And yes, is true that changing the order in which you load the packages solves also the problem, but that is not an encouraged solution - e.g. a colleague with whom you share the code, unaware of the bug, can easily change the order and things snap again; or your future self, forgetting about the "fix", falls in the same trap again - happens often to me :D

library(plyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:plyr':
#> 
#>     arrange, count, desc, failwith, id, mutate, rename, summarise,
#>     summarize
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
search()
#>  [1] ".GlobalEnv"        "package:dplyr"     "package:plyr"     
#>  [4] "package:stats"     "package:graphics"  "package:grDevices"
#>  [7] "package:utils"     "package:datasets"  "package:methods"  
#> [10] "Autoloads"         "package:base"

d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
rename(d, c("beta"="two", "gamma"="three"))
#> All arguments must be named

Created on 2019-04-20 by the reprex package (v0.2.1)

2) "conflicted" package to rescue

Such errors are relatively common, so the conflicted package can be very handy here. Once loaded, you can type the name of the function that gives you errors and you get some useful info to help you debug the issue - check this example below:

library(conflicted)
library(plyr)
library(dplyr)

rename
#> [conflicted] `rename` found in 2 packages.
#> Either pick the one you want with `::` 
#> * dplyr::rename
#> * plyr::rename
#> Or declare a preference with `conflict_prefer()`
#> * conflict_prefer("rename", "dplyr")
#> * conflict_prefer("rename", "plyr")

Created on 2019-04-20 by the reprex package (v0.2.1)