47  Common errors

This page includes a running list of common errors and suggests solutions for troubleshooting them.

47.1 Interpreting error messages

R errors can be cryptic at times, so Google is your friend. Search the error message with “R” and look for recent posts in StackExchange.com, stackoverflow.com, community.rstudio.com, twitter (#rstats), and other forums used by programmers to filed questions and answers. Try to find recent posts that have solved similar problems.

If after much searching you cannot find an answer to your problem, consider creating a reproducible example (“reprex”) and posting the question yourself. See the page on Getting help for tips on how to create and post a reproducible example to forums.

47.2 Common errors

Below, we list some common errors and potential explanations/solutions. Some of these are borrowed from Noam Ross who analyzed the most common forum posts on Stack Overflow about R error messages (see analysis here)

Typo errors

Error: unexpected symbol in:
"  geom_histogram(stat = "identity")+
  tidyquant::geom_ma(n=7, size = 2, color = "red" lty"

If you see “unexpected symbol”, check for missing commas

Package errors

could not find function "x"...

This likely means that you typed the function name incorrectly, or forgot to install or load a package.

Error in select(data, var) : unused argument (var)

You think you are using dplyr::select() but the select() function has been masked by MASS::select() - specify dplyr:: or re-order your package loading so that dplyr is after all the others.

Other common masking errors stem from: plyr::summarise() and stats::filter(). Consider using the conflicted package.

Error in install.packages : ERROR: failed to lock directory ‘C:\Users\Name\Documents\R\win-library\4.0’ for modifying
Try removing ‘C:\Users\Name\Documents\R\win-library\4.0/00LOCK’

If you get an error saying you need to remove an “00LOCK” file, go to your “R” library in your computer directory (e.g. R/win-library/) and look for a folder called “00LOCK”. Delete this manually, and try installing the package again. A previous install process was probably interrupted, which led to this.

Object errors

No such file or directory:

If you see an error like this when you try to export or import: Check the spelling of the file and filepath, and if the path contains slashes make sure they are forward / and not backward \. Also make sure you used the correct file extension (e.g. .csv, .xlsx).

object 'x' not found 

This means that an object you are referencing does not exist. Perhaps code above did not run properly?

Error in 'x': subscript out of bounds

This means you tried to access something (an element of a vector or a list) that was not there.

Function syntax errors

# ran recode without re-stating the x variable in mutate(x = recode(x, OLD = NEW)
Error: Problem with `mutate()` input `hospital`.
x argument ".x" is missing, with no default
i Input `hospital` is `recode(...)`.

This error above (argument .x is missing, with no default) is common in mutate() if you are supplying a function like recode() or replace_na() where it expects you to provide the column name as the first argument. This is easy to forget.

Logic errors

Error in if

This likely means an if statement was applied to something that was not TRUE or FALSE.

Factor errors

#Tried to add a value ("Missing") to a factor (with replace_na operating on a factor)
Problem with `mutate()` input `age_cat`.
i invalid factor level, NA generated
i Input `age_cat` is `replace_na(age_cat, "Missing")`.invalid factor level, NA generated

If you see this error about invalid factor levels, you likely have a column of class Factor (which contains pre-defined levels) and tried to add a new value to it. Convert it to class Character before adding a new value.

Plotting errors

Error: Insufficient values in manual scale. 3 needed but only 2 provided. ggplot() scale_fill_manual() values = c(“orange”, “purple”) … insufficient for number of factor levels … consider whether NA is now a factor level…

Can't add x object

You probably have an extra + at the end of a ggplot command that you need to delete.

R Markdown errors

If the error message contains something like Error in options[[sprintf("fig.%s", i)]], check that your knitr options at the top of each chunk correctly use the out.width = or out.height = and not fig.width= and fig.height=.


Consider whether you re-arranged piped dplyr verbs and didn’t replace a pipe in the middle, or didn’t remove a pipe from the end after re-arranging.

47.3 Resources

This is another blog post that lists common R programming errors faced by beginners