Reputation: 477
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.
Upvotes: 46
Views: 102817
Reputation: 11
I was having this trouble and solved it using:
df <- df %>% dplyr::rename(new_name=old_name)
Upvotes: 1
Reputation: 81
I was having a problem with the dplyr rename() function in the following format
d %>% rename(two=beta)
And was getting an error
Error in rename(d, two=beta) :
unused argument (two=beta)
I fixed it by changing the code to
d %>% dplyr::rename(two=beta)
Perhaps this was just a simple package masking problem then. This is not a direct answer to the original question, but hopefully it will help anyone receiving a similar error message to this one, as their search for a fix might lead them to this thread (as it did me).
Upvotes: 0
Reputation: 1577
same issue and tried many of the earlier suggestions, but the only thing that worked was restarting R/Rstudio. I should do that more as default.
Upvotes: 0
Reputation: 5924
I just stumbled upon this issue myself, so I thought I should share a solution.
There are two issues in your code. The first issue, which is causing the error, is that dplyr::rename
requires that the arguments be passed as var args, not as a single list. In other words, it should be:
rename(d, beta = "two", gamma = "three")
If you want to do that dynamically, you can use do.call as follows:
renames <- c(beta = 'two', gamma = 'three')
do.call(dplyr::rename, c(list(d), renames))
However, the other issue is that you have the renames backwards. The key should be the new column name and the value is the old column name. So you want to do this instead:
renames <- c(two = 'beta', three = 'gamma')
do.call(dplyr::rename, c(list(d), renames))
And using rlang's !!!
operator:
renames <- c(two = 'beta', three = 'gamma')
dplyr::rename(d, !!!renames)
But again, if you know the columns ahead of time, you can just use the first example.
Upvotes: 4
Reputation: 6416
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"))
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)
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)
Upvotes: 85
Reputation: 21
If you are using dplyr instead of plyr the grammar is slightly different. I was having this trouble and solved it using:
df <- df %>% rename(new_name=old_name)
Or in your case:
d <- d %>% rename(two=beta,three=gamma)
Upvotes: 2
Reputation: 521
Even though the question is old and it has been answered, I faced the same problem. Installing "reshape" package fixed the issue for me.
install.packages("reshape")
library(reshape)
Now the below code should work fine.
rename(d, c(beta="two", gamma="three"))
Thanks.
Upvotes: 0
Reputation: 309
I also had this error when I was trying to use rename_all on a grouped data set.
for instance:
as_tibble( mtcars ) %>% group_by(cyl) %>% rename_all(toupper)
will produce the error as stated by the OP.
To resolve, use ungroup()
as_tibble( mtcars ) %>% group_by(cyl) %>% ungroup() %>% rename_all(toupper)
Upvotes: 4
Reputation: 179408
You have to use unquoted names for the existing column name as well as the new name. Also, note that the new name appears on the left hand side.
Try this:
rename(d, two = beta, three = gamma)
alpha two three
1 1 4 7
2 2 5 8
3 3 6 9
Upvotes: 19