Error: attempt to apply non-function in R
When programming in R, you may occasionally encounter the following error:
Error: attempt to apply non-function
This error can be confusing, especially for beginners, because it doesn’t always clearly indicate where the problem lies.
In most cases, this error occurs when R encounters parentheses () immediately after an object that is not a function. Since R interprets parentheses as a function call, it attempts to execute the object as a function and fails.
In this tutorial, you’ll learn:
- What causes the “attempt to apply non-function” error
- How to reproduce the error
- Multiple ways to fix it
- Common real-world scenarios that trigger this error
- Best practices for avoiding similar issues
What Does “Attempt to Apply Non-Function” Mean?
In R, functions are objects that can be called using parentheses.
For example:
sqrt(25)
Output:
[1] 5
Here, sqrt is a function, so R successfully executes it.
However, when parentheses follow something that is not a function, R attempts to execute it and returns:
Error: attempt to apply non-function
Example 1: Reproducing the Error
Suppose we write:
5(7)
Output:
Error: attempt to apply non-function
Why Does This Happen?
R interprets the code as:
Call the object 5 as a function and pass 7 as an argument
Since 5 is a numeric value and not a function, R cannot execute it.
Solution 1: Use a Mathematical Operator
If your intention was to perform arithmetic, include the appropriate operator.
5 + 7
Output:
[1] 12
Or:
5 * 7
Output:
[1] 35
The error disappears because R now understands the operation being performed.
Example 2: Missing Multiplication Operator
A common mistake among new R users is forgetting the multiplication operator.
Incorrect
10(2)
Output:
Error: attempt to apply non-function
Correct
10 * 2
Output:
[1] 20
Unlike some calculators, R requires an explicit multiplication operator.
Example 3: Accidentally Overwriting a Function
Another common cause occurs when you overwrite a built-in function.
Create an Object Named mean
mean <- 100
Now try:
mean(c(1,2,3,4,5))
Output:
Error: attempt to apply non-function
Why?
You replaced the built-in mean() function with a numeric object.
When R encounters:
mean(...)
it attempts to call the value 100 as a function.
Fixing Overwritten Functions
Remove the object:
rm(mean)
Now:
mean(c(1,2,3,4,5))
Output:
[1] 3
The original function is restored.
Example 4: Incorrect Data Frame Access
Suppose we have:
df <- data.frame(
x = 1:5,
y = 6:10
)
Incorrect syntax:
df$x()
Output:
Error: attempt to apply non-function
Why?
df$x is a vector, not a function.
Correct
df$x
Output:
[1] 1 2 3 4 5
Example 5: Function Returning a Value
Consider:
x <- sum(1:10)
Now:
x(5)
Output:
Error: attempt to apply non-function
Because:
x
contains:
[1] 55
which is not callable as a function.
How to Debug This Error
When you see:
Error: attempt to apply non-function
check the following:
1. Verify Object Type
class(object_name)
or
typeof(object_name)
Example:
class(mean)
2. Check Whether It’s a Function
is.function(object_name)
Example:
is.function(mean)
Output:
FALSE
indicates that the object is no longer a function.
3. Inspect the Object
str(object_name)
This helps determine what R is attempting to execute.
Common Causes of This Error
| Cause | Example |
|---|---|
| Missing operator | 5(7) |
| Missing multiplication sign | 10(2) |
| Overwriting functions | mean <- 100 |
| Calling vectors as functions | x(5) |
| Calling data frame columns as functions | df$x() |
| Syntax mistakes | Missing commas or operators |
Best Practices
Avoid Naming Variables After Functions
Bad:
mean <- 100
sum <- 50
data <- 10
Better:
mean_value <- 100
total_sum <- 50
dataset <- 10
Use Explicit Operators
Always write:
5 * 7
instead of:
5(7)
Check Object Classes
Before calling an object:
is.function(my_object)
Use Descriptive Variable Names
Descriptive names reduce the risk of accidentally overwriting important functions.
Complete Example
# Incorrect code
5(7)
# Error:
# Error: attempt to apply non-function
# Correct code
5 + 7
# Output
[1] 12
Conclusion
The error:
Error: attempt to apply non-function
occurs when R tries to execute an object that is not actually a function.
The most common causes include:
- Missing mathematical operators
- Calling vectors as functions
- Overwriting built-in functions
- Syntax mistakes involving parentheses
When troubleshooting this error:
- Check whether the object is a function.
- Verify object classes.
- Ensure operators are included where necessary.
- Avoid using reserved function names as variable names.
By understanding how R interprets function calls, you can quickly identify and resolve this error, making your code more reliable and easier to debug.

