Common errors
What object not found, could not find function, non-numeric argument, subscript out of bounds and missing value where TRUE/FALSE needed mean and how to fix them.
Runs in Zerethon IDE · R 4.5
Official R documentation ↗
An error prints Error in <call> : <message> and stops the script (Execution halted). A warning prints Warning message: and the script goes on — but it usually means a result is not what you think.
object 'x' not found— a typo, a variable that was never assigned, or a column name used outside its data frame (ageinstead ofpeople$age).could not find function "foo"— a typo, a file not loaded withsource(), or a function from a package that is not available.non-numeric argument to binary operator— text used in arithmetic; convert withas.numeric().subscript out of bounds—[[i]]or a matrix index past the end.missing value where TRUE/FALSE needed— the condition ofifisNA; check withis.na()first.the condition has length > 1—ifgot a whole vector; useany(),all()orifelse().there is no package called 'dplyr'— only base R and the recommended packages are installed.- Warning
NAs introduced by coercion—as.numeric()met text that is not a number.
main.R
values <- c("10", "x", "7")
total <- 0
for (v in values) {
n <- suppressWarnings(as.numeric(v))
if (is.na(n)) {
cat(sprintf("skipped '%s'\n", v))
next
}
total <- total + n
}
cat(sprintf("total: %g\n", total))
result <- tryCatch(stop("boom"), error = function(e) conditionMessage(e))
print(result)
Output
skipped 'x' total: 17 [1] "boom"
tryCatch().