This page includes a running list of common errors and suggests solutions for troubleshooting them.
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.
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)
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
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)
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.
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.
# 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
replace_na() where it expects you to provide the column name as the first argument. This is easy to forget.
Error in if
This likely means an
if statement was applied to something that was not TRUE or FALSE.
#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.
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.
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
This is another blog post that lists common R programming errors faced by beginners