Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message when running simple 'rename' function in R

Tags:

r

rename

dplyr

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. 
like image 476
Mike Avatar asked May 31 '15 21:05

Mike


People also ask

How do I rename a function in R?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B”, again, you can run the following code: rename(dataframe, B = A) . That was it, we are getting ready to practice how to change the column names in R.

What package is the rename function in R?

Several R packages contain a rename function and with dplyr:: we tell R to use the rename function of the dplyr package.

How do I rename a variable in R?

I'll just say it once more: if you need to rename variables in R, just use the rename() function.

What is unused argument in R?

The “unused argument error in r” message occurs when you are using a function in R. It occurs when an argument used does not match the arguments in the argument list. This is particularly true when a different argument is added than what is in the list.

Why is my rename() function not working in R?

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).

How to handle errors in R programming?

In R Programming, there are basically two ways in which we can implement an error handling mechanism. Either we can directly call the functions like stop () or warning (), or we can use the error options such as “warn” or “warning.expression”. The basic functions that one can use for error handling in the code :

Why is my R script not opening a file?

There can be two reasons for this error to show up when running an R script: A file/connection can’t be opened because R can’t find it (mostly due to an error in the path) We are getting this error because we have specified the wrong path to the “dirPath” object in the code.

What is the difference between warning r and stop R function?

The warning R function generates a warning message. The stop R function generates an error message and stops executing the current R code. Basic R Syntaxes: You can find the basic R programming syntaxes of the message, warning, and stop functions below.


2 Answers

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)

like image 158
Valentin Avatar answered Nov 08 '22 00:11

Valentin


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 
like image 25
Andrie Avatar answered Nov 08 '22 01:11

Andrie