Fix 'could not find function' in R
The error could not find function "X" means R searched your workspace and every loaded package and found no function with that name. The usual cause is that the package owning the function was never loaded with library(), followed by plain typos.
library(dplyr) # the usual fix: load the owning package install.packages("dplyr") # one-time install if loading fails exists("select") # FALSE means R cannot see the name find("mean") # which package owns a visible function dplyr::select(mtcars, mpg) # call one function without attaching sessionInfo() # list the packages currently loaded
Need explanation? Read on for examples and pitfalls.
What this error means
R resolves every function name at the moment you call it. When you type select(mtcars, mpg), R walks its search path: first your workspace, then each attached package in order, then base R. If no environment on that path defines select, the call stops with this error. The quoted name in the message is exactly what R looked for, spelling and capitalization included.
The shortest way to trigger it is to call a package function in a fresh session:
select() lives in the dplyr package. A fresh R session attaches only the base packages, so the name does not exist yet, no matter how many times you installed dplyr in the past.
install.packages() copies files to disk once per machine. library() puts those functions on the search path, once per session. This error is almost always about loading, not installation.Diagnose it in 30 seconds
Two base functions locate the problem. exists() says whether R can see the name at all, and find() reports which attached package owns it:
Match what you observe against this table:
| Symptom | Likely cause | Fix |
|---|---|---|
exists("fn") is FALSE and you never ran library() |
Package not loaded this session | library(pkg) at the top of the script |
library(pkg) itself fails with "there is no package" |
Package not installed | install.packages("pkg") once |
| The quoted name looks almost right | Typo or wrong capitalization | Match the documented spelling exactly |
Works in your console, fails on knit or Rscript |
The fresh session never loads the package | Put library() calls inside the document |
The message names %>% |
Pipe package not attached | library(magrittr) or switch to the native pipe |
??select searches the documentation of every installed package, and getAnywhere("select") inspects loaded namespaces even when they are not attached.The four causes and their fixes
1. The package is not loaded
This is the cause in most real sessions. The function exists on disk, but the current session never attached its package. Load it, and the same call works:
Every script should load its packages at the top, one library() call per line. The packages chapter of the official An Introduction to R manual explains the attach mechanism in depth.
2. The package is not installed
Loading fails when there is nothing to load. If library(dplyr) answers with "there is no package called 'dplyr'", the package never made it onto this machine. Install it once, then load it every session:
This split trips people moving to a new computer or a fresh R upgrade: scripts full of library() calls fail until the packages are reinstalled.
3. A typo or wrong capitalization
R is case-sensitive, so Mean and mean are different names. The error message quotes the exact string R searched for, which makes typos easy to spot once you know to look:
Read the quoted name character by character. lenght(), summarise() versus summarize(), and stray autocomplete artifacts account for a surprising share of these errors.
4. The function is newer than your package version
A current tutorial plus an old installation produces a missing function. Packages gain functions over time; dplyr's reframe(), for example, only exists from version 1.1.0 onward. Check what you have:
If your version predates the function you need, update.packages() or a fresh install.packages("dplyr") brings it in. The same applies to base R itself: code using the native pipe fails on R older than 4.1.0.
library() in an early chunk.The %>% pipe case
The pipe is a function too, and it lives in a package. %>% comes from magrittr and is re-exported by dplyr and the rest of the tidyverse. Pasting piped code into a session with no packages loaded fails immediately:
Since R 4.1.0 there is also the native pipe |>, which is part of base R and needs no package at all. If you keep hitting this error in short scripts, the native pipe removes the dependency.
How to avoid it next time
A few habits make this error rare.
- Start every script with its
library()calls, grouped in one block, so a fresh session can run it top to bottom. - Use
pkg::fun()for one-off calls, such asdplyr::select(). The::form works without attaching and documents where the function comes from. - Restart R regularly and rerun from the top. A long-lived session hides missing
library()lines until the worst moment. - After following a tutorial, check versions with
packageVersion()before assuming your code is wrong.
NameError: name 'x' is not defined after a missing import. The fix is the same idea: load the module, or qualify the call with its package name.Try it yourself
Try it: The broken line below throws the missing-function error because of capitalization. Write the working call so ex_total holds the sum of 12, 6, and 9.
Click to reveal solution
Explanation: R is case-sensitive, so Sum does not match the base function sum. Lowercasing the name lets R find it on the search path.
FAQ
Why is R saying "could not find function %>%"?
Because the magrittr pipe was never attached in the current session. %>% is an operator function from the magrittr package, re-exported by dplyr and tidyverse. Run library(dplyr) or library(magrittr) before the piped code. On R 4.1.0 or newer you can also replace %>% with the native pipe |>, which belongs to base R and never triggers this error.
Why is my object not being found in R?
That is the sibling message, object 'x' not found, and it fires when a name is used as a value rather than called as a function. The line that creates the object never ran, ran with an error, or spelled the name differently. Rerun the creating line, then check with exists("x"). The object not found error guide walks through each case.
Is there a find function in R?
Yes. find("mean") returns which attached package defines a name, and exists("mean") answers whether the name is visible at all. For functions in installed but unattached packages, getAnywhere("fn") searches loaded namespaces, and ??fn searches the help index of everything installed. Together they answer both halves of this error: does the function exist, and where does it live.
What is the missing function in R?
In this error, the missing function is whatever name appears in quotes: R searched the workspace and all attached packages and found no function with that name. Separately, base R has a function called missing(), which tests inside a function whether a caller supplied an argument. The two are unrelated; missing() is about arguments, while this error is about name lookup.
Related R errors
Start with the parent guide to common R errors and how to read them. This error has a mirror: parentheses on a data object report a missing function, while brackets on a function report object of type closure is not subsettable. For the mechanics behind the fixes, see installing and loading packages, the pipe operator in R, and R environments for how the search path resolves names.