Unused argument
Matches: unused argument
WHAT THIS ERROR MEANS
You passed an argument to a function that doesn't accept it. This often happens from typos in argument names or using arguments from a different function.
WHY IT HAPPENS — 4 COMMON CAUSES
1. The argument name is misspelled
na.rm is the canonical trap — na.omit, na.rm = T, narm and na_rm all look plausible and none of them are the argument mean() accepts. R does partial matching on argument names, so na. would work, but anything that is not a prefix of the real name fails.
2. The argument belongs to a different function
stringsAsFactors belongs to data.frame() and read.csv(), not to as.data.frame() in every version. header belongs to read.csv() but not read_csv(). Arguments do not transfer between similarly-named functions from different eras or packages.
3. You are calling a different function than you think
If two attached packages export the same name, the later one masks the earlier. filter() from dplyr takes a data frame and conditions; filter() from stats takes a time series and a filter vector. Calling the wrong one produces this error with arguments that look entirely reasonable.
4. The S3 method for your object lacks that argument
Generic functions dispatch on class. summary() accepts different arguments for a data frame, an lm object and a factor. An argument documented on one method is not available on another, even though the call looks identical.
HOW TO DIAGNOSE WHICH ONE IT IS
- Run args(mean) for a one-line signature, or ?mean for the full argument list.
- Confirm which function you are actually calling with environment(filter) or by typing the bare name to see its namespace.
- For generics, use methods(summary) to list the methods, then ?summary.lm for the one that applies to your object.
- Check the object’s class with class(x) — dispatch follows it, and a tibble, data.table and data.frame do not always take the same arguments.
- When following a tutorial, compare packageVersion() against the version it was written for.
HOW TO FIX IT
1. Check the function's documentation with ?function_name.
2. Look for typos in argument names.
3. Make sure you're using the right function.
CODE EXAMPLES
mean(x = c(1, 2, 3), na.action = TRUE)
mean(x = c(1, 2, 3), na.rm = TRUE)
MORE SCENARIOS THAT TRIGGER THIS
Masked function from another package
library(dplyr) library(stats) # stats::filter now masks dplyr::filter filter(df, revenue > 100) # Error: unused argument (revenue > 100)
dplyr::filter(df, revenue > 100)
R prints masking conflicts when a package attaches, but they scroll past easily. Qualifying the call with :: is immune to load order and makes the dependency explicit.
Argument that belongs to a different reader
library(readr)
read_csv("data.csv", stringsAsFactors = FALSE)
# Error: unused argument (stringsAsFactors = FALSE)read_csv("data.csv") # readr never creates factorsread_csv() has no such argument because it does not convert strings to factors in the first place. Base read.csv() gained stringsAsFactors = FALSE as its default in R 4.0.0, so the argument is largely historical there too.
PACKAGE-SPECIFIC NOTES
Functions that accept ... are more forgiving in the wrong direction: they swallow misspelled arguments silently instead of raising this error, so ggplot(df, aes(x, y)) + geom_point(colour = "red", siz = 3) draws points at the default size with no complaint. That makes a silent no-op the failure mode in ggplot2 and many tidyverse functions, which is harder to spot than the error itself. When a ggplot2 setting appears to be ignored, suspect a typo in an argument name.
FUNCTIONS WORTH KNOWING
args()formals()methods()getAnywhere()class()match.arg()FREQUENTLY ASKED QUESTIONS
Why does R accept mean(x, na.) but not mean(x, narm = TRUE)?
R does partial matching on argument names: na. is an unambiguous prefix of na.rm, so it matches. narm is not a prefix of anything mean() accepts, so it falls through to the unused-argument error.
How do I see which package a function came from?
Type the function name without parentheses. R prints the body followed by a bytecode and namespace line, for example <environment: namespace:dplyr>. environment(fn) gives the same answer directly.
Why do I only get this error for some data frames?
Because dispatch follows class. If one object is a plain data.frame and another is a tbl_df or data.table, they may resolve to different methods with different argument lists.
Still stuck?
Paste your code in RChat and the AI will fix this error in context.