In R, if you try to call a function and pass arguments that are not defined, you will raise the error: unused arguments.
The error message will indicate which argument is unnecessary, and you can remove it to solve the error.
If you are using a built-in function, you can use the question mark ?
before the function name to get the R documentation for the function. The documentation will give you the parameters for the function.
The error can also occur if you have two libraries installed with the same function. To solve this error, you can use the package’s name (namespace) with the required function, for example, dplyr::select.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
Example: Unexpected Additional argument
Let’s look at a user-defined R function that takes two arguments.
exponent <- function(x, n){ result = x ** n return(result) }
The function raises the first number x
to the power of the second number n
and returns the result. Next, we will try to call the function:
exponent(x=2, n=3, s=10)
Let’s run the code to see what happens:
Error in exponent(x = 2, n = 3, s = 10) : unused argument (s = 10)
We raise an error because the exponent function only expects two arguments, but we passed an additional third argument.
Solution
We can solve this error by removing the extra argument indicated by the error message. Let’s look at the revised code:
exponent(x=2, n=3)
Let’s run the code to see the result:
[1] 8
We successfully called the function and returned the result of 2 to the power of 3.
Example: error in select unused arguments
Let’s look at an example where we want to summarize a variable in the mtcars
built-in dataset. First, let’s see the first six rows and column headers of the dataset:
head(mtcars)
mpg cyl 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
Next, we will attempt to find the average horsepower (hp) grouped by cylinders (cyl):
library(dplyr) library(MASS) > mtcars %>% + select(cyl, hp) %>% + group_by(cyl) %>% + summarize(avg_hp = mean(hp))
Let’s run the code to see what happens:
Error in select(., cyl, hp) : unused arguments (cyl, hp)
The R interpreter raises the error because we have installed both the MASS
and dplyr
packages and the select()
function from each package clashes.
Solution
We can solve this error by specifying the package’s name before calling the necessary function with two colons between the package name and the function. Let’s look at the revised code:
library(dplyr) library(MASS) > mtcars %>% + dplyr::select(cyl, hp) %>% + group_by(cyl) %>% + summarize(avg_hp = mean(hp))
Let’s run the code to see the result:
# A tibble: 3 × 2 cyl avg_hp <dbl> <dbl> 1 4 82.6 2 6 122. 3 8 209.
We successfully calculated the average horsepower (hp) grouped by cylinders (cyl).
Summary
Congratulations on reading to the end of this tutorial! The R error: unused arguments can occur when you provide additional arguments to a function that R does not expect. The error message will indicate which argument is not required. The error can also occur if there are clashing functions from two or more imported libraries. You can specify the package name before calling the function to solve this error.
For further reading on R related errors, go to the articles:
- How to Solve R Error: $ operator is invalid for atomic vectors
- How to Solve R Error: Subscript out of bounds
- How to Solve R Error in apply: dim(X) must have a positive length
- How to Solve R Error: Cannot add ggproto plots together
Go to the online courses page on R to learn more about coding in R for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.