Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

Error message when running simple 'rename' function in R

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.
question from:https://stackoverflow.com/questions/30562819/error-message-when-running-simple-rename-function-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...