
The “could not find function” error is one of the most common errors R students hit in their first weeks of using the language. The error message looks intimidating, but the cause is almost always simple. R is telling you that the function you typed is not currently loaded, not currently spelled correctly, or not currently visible in the place you typed it.
There are six distinct reasons R produces this error. Each one has a specific fix. This post walks through all six causes one by one, with the exact code that triggers each error and the exact code that resolves it.
The error itself looks like this in the R console:
Error in ggplot(data = mtcars, aes(x = wt, y = mpg)) :could not find function "ggplot"
The function name in quotes at the end of the error message is your starting clue. Whatever appears there is the function R cannot find. The next step is figuring out why.
What the error actually means
R holds every function in a structure called the environment. When you type a function name, R searches the environment for a matching name and runs the function it finds. If the search returns nothing, R produces the “could not find function” error.
The function exists somewhere on your computer, perhaps inside a package you installed once or in a script you wrote yesterday. None of that helps if it is not currently loaded into the environment R is searching right now.
Six things commonly go wrong, in roughly the order you encounter them as a beginner. Working through them in order solves the error in almost every case.
Cause 1: The package is installed but not loaded
What is happening
This is the most common cause for beginners by a wide margin. You installed a package once, some time ago, and the function lives inside that package. But installing a package and loading a package are two different actions. Installation puts the package on your hard drive. Loading puts the package’s functions into the current R session.
Every time R starts a fresh session, the only packages loaded by default are the base ones (base, stats, graphics, and a handful of others). Everything else has to be loaded by hand.
How to fix it
Add a library() call to the top of your script, naming the package the function lives in:
library(ggplot2)ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
If you are not sure which package contains the function, run ?function_name in the R console. The help page that opens shows the package name in the top-left corner.
If the package is not installed at all, library() produces a different error: there is no package called ‘ggplot2’. In that case, install it first with install.packages(“ggplot2”), then load it.
Cause 2: The function name is misspelled
What is happening
R is case-sensitive. Mean() is not the same function as mean(), and ggplot2() is not the same function as ggplot(). One typo in the function name is enough to trigger the error, even when everything else in the line is correct.
Common spelling mistakes for new R users include:
Mean(x) # capital M instead of lowercaseRead.csv(...) # capital R, dot in wrong placeggPlot(...) # camelCase instead of all lowercaselenght(x) # length spelled incorrectlysummery(x) # summary spelled incorrectly
How to fix it
Re-read the function name carefully against the documentation or the official package website. RStudio helps here: as you type a function name, RStudio shows a popup with matching function names from loaded packages. If the popup shows your intended function, the spelling is correct. If the popup is empty or shows something different, the spelling is wrong.
For functions with periods or underscores in their names, like read.csv() or read_csv(), the punctuation matters as much as the letters. read.csv() lives in base R, while read_csv() with the underscore lives in the readr package. They behave slightly differently.
Cause 3: Two packages have a function with the same name
What is happening
This one catches almost every R student at some point. Different packages sometimes use the same function name for different functions. The most famous example is filter(), which exists in both the dplyr package (for filtering rows of a data frame) and the stats package (a base R function for time-series filtering). Another is select(), which appears in dplyr and the MASS package.
When you load both packages, only one of the two functions is visible at a time. Whichever was loaded most recently wins. The other gets hidden, and trying to use it throws the “could not find function” error or, worse, calls the wrong function silently.
How to fix it
Use the double colon notation to call the function from the specific package:
dplyr::filter(mtcars, mpg > 20) # explicitly the dplyr filterstats::filter(my_timeseries) # explicitly the stats filter
The package_name::function_name syntax bypasses the namespace conflict entirely. R goes straight to the named package and calls the named function from there. This is also useful inside scripts you plan to share, because it makes the code’s intent explicit to the next reader.
RStudio shows you about namespace conflicts when you load a package. Watch the console for messages that look like this:
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':filter, lag
That message is R telling you which functions from the previously loaded stats package are now hidden behind the newly loaded dplyr versions. If you need both, use the double colon notation.
Cause 4: The package version is outdated
What is happening
Functions get added, renamed, and occasionally removed across package versions. Code from a tutorial published two years ago sometimes calls a function that no longer exists in the current version of the package. Or you have an old version installed locally, and the function has not yet been added to your version.
A common example: the tidyverse replaced gather() and spread() with pivot_longer() and pivot_wider() a few years ago. The old functions still exist for backwards compatibility, but new tutorials use the new names. If you are following a tutorial that uses old names on a freshly installed tidyr, or new names on an old tidyr, one of them throws the “could not find function” error.
How to fix it
Check the package version with packageVersion():
packageVersion("tidyr")# [1] '1.3.0'
Compare your installed version with the package’s documentation page on CRAN or its GitHub repository. If your version is older, update the package:
install.packages("tidyr")
Running install.packages() on a package you already have installed re-installs the latest version. After updating, restart the R session (Session menu, then Restart R in RStudio) before loading the package again. Restarting clears any old version that is still cached in memory.
Cause 5: The function is defined inside another function
What is happening
R has scoping rules. A function defined inside another function is only visible inside the parent function. If you try to call the inner function from the global environment, R cannot see it, and the “could not find function” error is what you get.
Here is an example:
outer_function <- function(x) {helper <- function(y) y * 2helper(x)}
outer_function(5) # works, returns 10helper(5) # error: could not find function "helper"
The helper function exists, but only inside outer_function. Once outer_function finishes running, the helper disappears.
How to fix it
Define the function at the top level of your script instead, where everything else can see it:
helper <- function(y) y * 2
outer_function <- function(x) {helper(x)}
outer_function(5) # workshelper(5) # also works now
This pattern is useful when you have small utility functions that you want to call from multiple places. Define them once at the top of the script, then call them anywhere afterwards.
Cause 6: A typo in your own custom function name
What is happening
Custom functions you wrote yourself are subject to the same case-sensitivity and spelling rules as built-in functions. If you defined a function as calculateMean and call it as calculatemean, R does not find it. The error message is identical to the package-not-loaded version, which makes the cause harder to spot.
This cause also covers a subtler version of the same problem: forgetting to run the line that defines the function in the first place. RStudio executes script lines one at a time when you press Ctrl+Enter (or Cmd+Enter on Mac). If you skipped over the definition line, the function does not exist in the session yet, even though it is written in the script in front of you.
How to fix it
Check the function exists in the current session by typing its name (without parentheses) into the console:
calculateMean
If the function exists, R prints out the function definition. If it does not, R returns the same “could not find function” error or a similar “object not found” error. In that case, scroll up in your script, find the function definition, and run that line first.
To run an entire script in one go (which guarantees every function definition gets executed in order), use Ctrl+Shift+Enter in RStudio, or click the Source button at the top of the script editor.
Quick diagnostic checklist
When the error appears, work through these checks in order:
1. Read the function name in the error message carefully. Double-check the spelling, the case, and any periods or underscores.
2. Confirm the package is loaded with library(package_name). If the package is not installed, install it first.
3. If two packages have a function with the same name, use the package::function notation to specify which one you want.
4. Check the package version with packageVersion(). Update if outdated.
5. If the function is custom, type the name without parentheses to check whether it exists in the session. If not, run the definition line first.
6. Restart the R session (Session menu, Restart R) and re-run the script from the top. A clean restart resolves a surprising number of environment-related errors.
When the error happens repeatedly
If the error keeps coming back even after the obvious fixes, the cause is usually deeper. The package you depend on has a broken installation, a dependency conflict, or a version mismatch with another package in your library.
The cleanest reset is to remove the problem package entirely, restart R, and reinstall it:
remove.packages("package_name")# Restart R: Session menu > Restart Rinstall.packages("package_name")library(package_name)
If even that does not fix the error, the issue is likely with a different package that package_name depends on. Check the package’s documentation page on CRAN for its required dependencies and confirm each one is installed and up to date.
This kind of dependency debugging is genuinely time-consuming, and missing a deadline because of an environment issue is the worst kind of marks loss. R programming homework help from a verified expert resolves environment errors in a fraction of the time it takes to work through the dependency tree alone.
Once the error is fixed
With the function loading correctly, the rest of your assignment workflow continues from where it stopped. If your code was about to fit a regression, Linear regression in R walks through the full process from lm() to interpreting the summary() output. If your code was about to check the assumptions of a regression you already fitted, Regression assumptions in R covers the four diagnostic plots in detail.
The “could not find function” error is one of the most frustrating errors for a new R user, because it stops your work entirely until it is resolved. Knowing the six causes and their fixes turns a session-killer into a 30-second pause.