Simple English Explanation:
Ahh, the ubiquitous “Error: could not find function” error that you will regularly encounter when writing code in R. It was actually ranked the most common “class” of R error message in this study.
This is generally one of the following:
- A typographical error in your code (function name spelled incorrectly)
- Failure to attach a package or dependency to the work-space before invoking a function
- The even more banal failure to install a required package
- Installing multiple versions of the same function in the same namespace (different packages)
- Finally, with a truly antique version of R – failure to include a core language feature
- Namespace 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 function, which is a powerful cue on where to look. With regards to finding the specific problem….
- Check for typographical errors in function names. This will explain about 80% of your errors.
- Verify that the package containing the function was installed and attached to the work-space. This should resolve about 80% of the remaining errors.
- Make sure you didn’t install two packages with the same function name. Another 80% addressed.
- In the remaining cases (~0.8% = 20% of 20% of 20%), you will need deeper spelunking to find the missing connection.
A lot of these errors seem to involve the “%>%” operator, which not actually part of Base R. 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 data manipulations. dplyr leans on this particular dependency fairly heavily.
ggplot 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 package 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 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 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>