Error: could not find function

Simple English Explanation:

Ahh, the ubiquitous ‘Error: could not find function’ error that you will regularly encounter when writing code in R programming. It was actually ranked the most common “class” of R error message in this study.

This output is generally the result of one of the following issues with the specified function in your R code:

  • A typographical error in your code (function name spelled incorrectly)
  • Failure to attach a package or dependency to the work-space before invoking one of your newly added functions
  • The even more banal failure to install a required package or file
  • Installing multiple versions of the same function in the same namespace (different packages)
  • Finally, with a truly antique R version – failure to include a core language feature, any later version should have the correct features and library functions, unless you are running an outdated R version it is likely an issue with that specific R code chunk
  • Namespace load or working directory related errors
  • Inability to access a non-R dependency (in rare circumstances)

Recommended Troubleshooting Approach:

R will generally provide you with the name of the missing specified function, which is a powerful cue on where to look. With regards to finding the specific problem code chunk….

  1. Check for typographical errors in function names. You may have mistyped the name of one of your library functions, and simply retyping the input value with the correct spelling of the object should produce a working output in your R script. This will explain about 80% of your errors.
  2. Verify that the package containing the function was installed and attached to the work-space. This should resolve about 80% of the remaining errors.
  3. Make sure you didn’t install two packages with the same function name. Another 80% addressed.
  4. In the remaining cases (~0.8% = 20% of 20% of 20%), you will need deeper spelunking to find the missing connection.

Specific Situations

A lot of these errors seem to involve the “%>%” operator, which not actually a regular expression Base R programming. You will need to install and attach the magrittr package to get this functionality. Most people who hit this bug are probably trying to use the dplyr package for heavy duty dataset, data frame, excel file, or graph manipulations. dplyr leans on this particular dependency fairly heavily.

The ggplot2 package is another frequent flyer for this error. If you are getting messages about ggplot, this module was probably included to support graphing data. We actually have other materials on ggplot. In any event, include the missing value of the package in your R script and you should be good to go.

Other common candidates:

vif  – statistical processing – not part of base R, install the car package

corpus – natural language processing – versions are present in a couple of different packages. Make sure you included the right one.

count – likely a problem with dplyr. In some cases, this may be a problem with loading another package that also defines a count function (plyr) after you’ve loaded dplyr, creating a namespace issue. Here is a potential solution example if you’ve got a namespace collision (read to the bottom).

select – another function that shows up in multiple packages, but dplyr is probably involved in most situations where you get this error. Root cause is either namespace load collision or lack of dependencies.

Need more help? Check out our list of common R error messages for more ideas….

Error: Error: could not find function <function name>

Scroll to top