50 Most Common R Error Messages: What They Mean & How to Fix Them
R error messages can be cryptic. This is your quick-reference guide to the 50 errors you'll encounter most often, organized by category. Each entry explains what the error means in plain English and gives you a one-line fix.
Bookmark this page. When you hit an error, Ctrl+F to find it here. For the most common errors, we have dedicated deep-dive posts with step-by-step fixes linked in each section.
Syntax Errors
These errors mean R couldn't even parse your code — something is structurally wrong.
# Error 1: unexpected symbol
# Cause: Missing operator or comma
# Bad: x y <- 5
# Fix: x <- 5 OR x_y <- 5
# Error 2: unexpected '}'
# Cause: Mismatched braces or extra closing brace
# Bad: if (TRUE) { x <- 1 }}
# Fix: if (TRUE) { x <- 1 }
# Error 3: unexpected ')'
# Cause: Extra closing parenthesis or missing argument
# Bad: mean(c(1,2,3)))
# Fix: mean(c(1,2,3))
# Error 4: unexpected string constant
# Cause: Missing comma between arguments
# Bad: c("a" "b")
# Fix: c("a", "b")
# Error 5: unexpected 'else'
# Cause: else on a new line without { }
# Bad:
# if (TRUE) x <- 1
# else x <- 2
# Fix:
# if (TRUE) { x <- 1 } else { x <- 2 }
cat("Syntax errors are always about structure, not logic.\n")
cat("Fix: count your parentheses, braces, and commas.\n")
#
Error
Cause
Quick Fix
1
unexpected symbol
Missing operator/comma
Add the missing , or operator
2
unexpected '}'
Extra closing brace
Match all { with }
3
unexpected ')'
Extra closing paren
Match all ( with )
4
unexpected string constant
Missing comma
Add , between arguments
5
unexpected 'else'
else on new line
Use { } else { } on same line or wrap in braces
6
unexpected '='
= in wrong context
Use <- for assignment, == for comparison
7
unexpected input
Invalid character (often curly quotes)
Replace with straight quotes
8
unexpected end of input
Unclosed string/paren/brace
Close all open delimiters
Object & Name Errors
These errors mean R can't find something you referenced.
# Error 9: object 'x' not found
# Cause: Variable doesn't exist (typo, not loaded, wrong scope)
# Fix: Check spelling, make sure it's been created
# Error 10: could not find function "fn"
# Cause: Package not loaded or function name misspelled
# Fix: library(package_name) or check spelling
# Demonstrate:
tryCatch(
nonexistent_var + 1,
error = function(e) cat("Error 9:", conditionMessage(e), "\n")
)
tryCatch(
nonexistent_function(),
error = function(e) cat("Error 10:", conditionMessage(e), "\n")
)
#
Error
Cause
Quick Fix
9
object 'x' not found
Undefined variable
Check spelling, create the variable first
10
could not find function "fn"
Package not loaded
library(pkg) or pkg::fn()
11
unused argument
Wrong argument name
Check ?function_name for valid args
12
argument "x" is missing, with no default
Required arg not supplied
Pass the required argument
Type & Coercion Errors
These mean you're using the wrong type of data.
# Error 13: non-numeric argument to binary operator
x <- "hello"
tryCatch(
x + 1,
error = function(e) cat("Error 13:", conditionMessage(e), "\n")
)
cat("Fix: check types with class() or is.numeric()\n\n")
# Error 14: argument is not numeric or logical
tryCatch(
mean(c("a", "b", "c")),
warning = function(w) cat("Error 14 (warning):", conditionMessage(w), "\n")
)
cat("Fix: convert with as.numeric() first\n")
#
Error
Cause
Quick Fix
13
non-numeric argument to binary operator
Math on non-numeric
Convert with as.numeric()
14
argument is not numeric or logical
Wrong type to math fn
Check/convert input type
15
NAs introduced by coercion
Can't convert to number
Clean data before converting
16
invalid 'type' (character) of argument
Character where number needed
Use as.numeric()
17
cannot coerce type 'closure' to vector
Using function name without ()
Add parentheses: fn() not fn
18
comparison is possible only for atomic and list types
Comparing incompatible types
Check both objects' types
Subsetting & Indexing Errors
These occur when accessing elements of vectors, lists, or data frames incorrectly.
# Error 26: arguments imply differing number of rows
tryCatch(
data.frame(a = 1:3, b = 1:5),
error = function(e) cat("Error 26:", conditionMessage(e), "\n")
)
# Error 27: duplicate 'row.names' are not allowed
tryCatch(
data.frame(x = 1:3, row.names = c("a", "a", "b")),
error = function(e) cat("Error 27:", conditionMessage(e), "\n")
)
#
Error
Cause
Quick Fix
26
arguments imply differing number of rows
Unequal column lengths
Make all columns same length
27
duplicate 'row.names' are not allowed
Non-unique row names
Use make.unique() or remove row names
28
object of type 'closure' is not subsettable
Subsetting a function
You forgot () — call the function first
29
cannot allocate vector of size X
Not enough memory
Reduce data size or increase memory
Package & Loading Errors
#
Error
Cause
Quick Fix
30
there is no package called 'x'
Not installed
install.packages("x")
31
package 'x' was built under R version Y
Version mismatch
Update R or reinstall package
32
namespace 'x' is imported by 'y'
Dependency conflict
Update both packages
33
package 'x' is not available for this version of R
Too old R
Update R
ggplot2 Errors
# Common ggplot2 errors (shown as strings since ggplot2 may not be loaded)
cat("Error 34: ggplot2 doesn't know how to deal with data of class 'function'\n")
cat(" Cause: Passed function name, not data. Fix: use data(), not data\n\n")
cat("Error 35: object 'x' not found (in ggplot aes)\n")
cat(" Cause: Column name doesn't exist. Fix: check names(data)\n\n")
cat("Error 36: Discrete value supplied to continuous scale\n")
cat(" Cause: Character column on numeric axis. Fix: convert or change scale\n")
#
Error
Cause
Quick Fix
34
doesn't know how to deal with class 'function'
Function name instead of data
Call the function: data() not data
35
object 'x' not found (in aes)
Column not in data
Check names(your_data)
36
Discrete value supplied to continuous scale
Character on numeric axis
as.numeric() or use discrete scale
37
Aesthetics must be either length 1 or same as data
Vector length mismatch
Put data in the data frame
38
stat_count() can only have an x or y aesthetic
Both x and y with geom_bar
Use geom_col() for precomputed values
Modeling & Statistical Errors
#
Error
Cause
Quick Fix
39
contrasts can be applied only to factors with 2+ levels
Factor has 1 level
Remove or combine single-level factors
40
singular fit / system is computationally singular
Perfectly collinear predictors
Remove redundant variables
41
NA/NaN/Inf in foreign function call
Missing/infinite values in model data
Remove NAs with na.omit()
42
variable lengths differ
Predictors different lengths
Ensure all variables same length
43
formula is missing
lm() without formula
Use lm(y ~ x, data = df)
File & I/O Errors
#
Error
Cause
Quick Fix
44
cannot open the connection
File doesn't exist or wrong path
Check file.exists(path)
45
no lines available in input
Empty file
Check file has content
46
more columns than column names
CSV parsing issue
Set sep, header, skip correctly
47
invalid multibyte string
Encoding issue
Use fileEncoding = "UTF-8"
Miscellaneous Errors
#
Error
Cause
Quick Fix
48
the condition has length > 1
Vector in if()
Use any(), all(), or if(x[1])
49
attempt to apply non-function
() on non-function object
Check variable isn't overwriting a function
50
recursive default argument reference
Default arg references itself
Fix the circular default
Quick Debugging Checklist
# When you hit ANY error, try these steps:
cat("1. READ the error message carefully\n")
cat("2. Check the LINE NUMBER (if shown)\n")
cat("3. Run str() on your objects:\n")
x <- data.frame(a = 1:3, b = letters[1:3])
str(x)
cat("\n4. Check types with class():\n")
cat(" class(x):", class(x), "\n")
cat("\n5. Check dimensions:\n")
cat(" dim(x):", dim(x), "\n")
cat(" length(x):", length(x), "\n")
cat("\n6. Look at first few rows:\n")
head(x)
Practice Exercises
Exercise 1: Fix the Errors
# Exercise: Each line below has an error. Fix them all.
# Uncomment each line, fix it, and run.
# Line 1: c(1 2 3)
# Line 2: data.frame(x = 1:3, y = 1:5)
# Line 3: "hello" + 5
# Line 4: x <- c(1,2,3); x[[5]]
# Line 5: df <- data.frame(a=1:3); df[, "b"]
# Write your fixes below:
Click to reveal solution
```r
# Line 1: Missing commas
fixed_1 <- c(1, 2, 3)
cat("Line 1:", fixed_1, "\n")
# Line 2: Differing row counts
fixed_2 <- data.frame(x = 1:5, y = 1:5) # Make same length
cat("Line 2:", nrow(fixed_2), "rows\n")
# Line 3: Non-numeric argument
fixed_3 <- paste0("hello", 5) # Or: as.numeric("hello") + 5 if it were "5"
cat("Line 3:", fixed_3, "\n")
# Line 4: Subscript out of bounds
x <- c(1, 2, 3)
fixed_4 <- x[min(5, length(x))] # Safe indexing
cat("Line 4:", fixed_4, "\n")
# Line 5: Undefined column
df <- data.frame(a = 1:3)
# Option A: Use a column that exists
cat("Line 5:", df[, "a"], "\n")
# Option B: Add the column first
df$b <- 4:6
cat("Line 5 (after adding b):", df[, "b"], "\n")
**Explanation:** Each fix addresses a different error category: syntax (missing comma), dimension mismatch, type error, index out of bounds, and undefined column.
Exercise 2: Error Detective
# Exercise: This code produces an error. Without running it first,
# predict which line fails and what error you'll get.
# Then run it to check.
process <- function(data) {
data$total <- data$price * data$quantity
data$category <- toupper(data$category)
data$discount <- ifelse(data$total > 100, 0.1, 0)
data$final <- data$total * (1 - data$discount)
data
}
orders <- data.frame(
price = c(25, 50, 10),
quantity = c(4, 3, 12),
category = c("electronics", "books", "food")
)
result <- process(orders)
print(result)
# Prediction: Does it error? If so, which line and what error?
Click to reveal solution
```r
# This code actually works without errors!
# If you predicted an error, common wrong guesses:
# - toupper on a factor (but data.frame with stringsAsFactors=FALSE by default in R 4.0+)
# - The code is actually correct.
process <- function(data) {
data$total <- data$price * data$quantity
data$category <- toupper(data$category)
data$discount <- ifelse(data$total > 100, 0.1, 0)
data$final <- data$total * (1 - data$discount)
data
}
orders <- data.frame(
price = c(25, 50, 10),
quantity = c(4, 3, 12),
category = c("electronics", "books", "food")
)
result <- process(orders)
print(result)
cat("\nNo error! The trick was to read carefully before assuming.\n")
cat("Lesson: Not every 'find the bug' exercise has a bug.\n")
**Explanation:** This exercise tests whether you jump to conclusions. Reading the error message (or lack thereof) carefully is the first debugging skill.
Summary
Category
Most Common Error
Typical Cause
Syntax
unexpected symbol
Missing comma or operator
Object
object 'x' not found
Typo or variable not created
Type
non-numeric argument
Wrong data type
Subsetting
subscript out of bounds
Index too large
Data frame
undefined columns selected
Wrong column name
Package
there is no package called
Not installed
ggplot2
object not found in aes
Column not in data
Modeling
contrasts can be applied...
Single-level factor
FAQ
Why are R error messages so cryptic?
Many R error messages come from deep inside C code and were written for developers, not users. The R community is working on improving error messages. Packages like rlang produce much friendlier errors. The tidyverse packages generally have the best error messages.
How do I search for an R error online?
Copy the error message (without your specific variable names), put it in quotes, and search: "non-numeric argument to binary operator" R. Stack Overflow has answers for almost every R error.
What does "non-standard evaluation" have to do with errors?
Many tidyverse errors about "objects not found" happen because of non-standard evaluation (NSE). Functions like dplyr::filter() look for column names inside the data frame, not in your environment. If you pass a variable that isn't a column name, you get confusing errors.
What's Next?
For deep dives into the most common errors, see:
R Error: object 'x' not found — 7 causes and step-by-step fixes
R Error: subscript out of bounds — what it means and how to solve it
R Error: undefined columns selected — data frame subsetting fix
R Error: replacement has length zero — the NA assignment bug