Thomas Lab
  • Home
  • About
  • Blog
  • White Papers
  • Research
  • Teaching
  • Misc

On this page

  • 1 Object Not Found Errors
    • 1.1 Error: object 'x' not found
  • 2 Package/Function Not Found
    • 2.1 Error: could not find function "function_name"
  • 3 Data Type Errors
    • 3.1 Error: non-numeric argument to mathematical function
  • 4 Subsetting Errors
    • 4.1 Error: subscript out of bounds
  • 5 Missing Values Issues
    • 5.1 Error: missing values in object
  • 6 File Reading Errors
    • 6.1 Error: cannot open the connection
  • 7 Memory Issues
    • 7.1 Error: cannot allocate vector of size X
  • 8 Package Installation Issues
    • 8.1 Error: package installation failed
  • 9 General Debugging Tips
  • 10 Prevention Strategies

Fixing Common R Errors: A Troubleshooting Guide

Step-by-step solutions for frequent R programming problems

R
troubleshooting
debugging
guide
Practical solutions for the most common R errors encountered by data scientists and analysts.

1 Object Not Found Errors

1.1 Error: object 'x' not found

Problem: R can’t find the variable or function you’re trying to use.

Common Causes: - Typo in variable name (R is case-sensitive) - Variable not created yet - Variable created in different environment

Solutions:

  1. Check spelling and case:

    # Wrong
    myData <- data.frame(x = 1:5)
    print(mydata)  # Error: object 'mydata' not found
    
    # Correct
    print(myData)
  2. List current objects:

    ls()  # See what objects exist
  3. Check if package is loaded:

    # If using dplyr functions
    library(dplyr)

2 Package/Function Not Found

2.1 Error: could not find function "function_name"

Problem: Function doesn’t exist or package isn’t loaded.

Solutions:

  1. Install missing package:

    install.packages("package_name")
    library(package_name)
  2. Use package::function notation:

    # Instead of loading entire package
    dplyr::filter(data, condition)
  3. Check function spelling:

    # Wrong
    summery(data)
    
    # Correct
    summary(data)

3 Data Type Errors

3.1 Error: non-numeric argument to mathematical function

Problem: Trying to do math on text or factor data.

Solutions:

  1. Check data types:

    str(data)          # See structure
    class(data$column) # Check specific column
  2. Convert to numeric:

    # If column should be numeric
    data$column <- as.numeric(data$column)
    
    # Handle warnings about NAs
    data$column <- as.numeric(as.character(data$column))
  3. Remove non-numeric characters:

    # Remove dollar signs, commas, etc.
    data$price <- as.numeric(gsub("[^0-9.]", "", data$price_text))

4 Subsetting Errors

4.1 Error: subscript out of bounds

Problem: Trying to access row/column that doesn’t exist.

Solutions:

  1. Check dimensions:

    dim(data)        # Rows and columns
    nrow(data)       # Number of rows
    ncol(data)       # Number of columns
  2. Use safe subsetting:

    # Instead of data[100, ] which might not exist
    if (nrow(data) >= 100) {
      result <- data[100, ]
    }
  3. Check column names:

    names(data)      # See actual column names
    "column_name" %in% names(data)  # Check if column exists

5 Missing Values Issues

5.1 Error: missing values in object

Problem: Functions can’t handle NA values.

Solutions:

  1. Remove NAs explicitly:

    mean(data$column, na.rm = TRUE)
    sum(data$column, na.rm = TRUE)
  2. Check for missing values:

    sum(is.na(data$column))    # Count NAs
    complete.cases(data)       # Rows without NAs
  3. Handle missing data:

    # Remove rows with any NA
    clean_data <- na.omit(data)
    
    # Remove rows with NA in specific column
    clean_data <- data[!is.na(data$column), ]

6 File Reading Errors

6.1 Error: cannot open the connection

Problem: R can’t find or access the file.

Solutions:

  1. Check file path:

    getwd()                    # Current directory
    file.exists("filename.csv") # Check if file exists
  2. Use correct path separators:

    # Windows - use forward slashes or double backslashes
    data <- read.csv("C:/Users/name/data.csv")
    # or
    data <- read.csv("C:\\Users\\name\\data.csv")
  3. Check file permissions:

    # Make sure file isn't open in Excel
    # Check that you have read permissions

7 Memory Issues

7.1 Error: cannot allocate vector of size X

Problem: Not enough memory for the operation.

Solutions:

  1. Check memory usage:

    memory.size()      # Current usage (Windows)
    object.size(data)  # Size of specific object
  2. Free up memory:

    rm(large_object)   # Remove unneeded objects
    gc()               # Force garbage collection
  3. Work with smaller chunks:

    # Read file in chunks
    library(readr)
    data <- read_csv_chunked("large_file.csv", 
                            chunk_size = 1000,
                            callback = DataFrameCallback$new())

8 Package Installation Issues

8.1 Error: package installation failed

Problem: Package won’t install due to dependencies or system issues.

Solutions:

  1. Update R and packages:

    update.packages(ask = FALSE)
  2. Install from different repository:

    # Try different CRAN mirror
    install.packages("package_name", repos = "https://cloud.r-project.org")
    
    # Install from GitHub
    devtools::install_github("user/package")
  3. Install dependencies manually:

    # Install suggested dependencies
    install.packages("package_name", dependencies = TRUE)

9 General Debugging Tips

  1. Use debugging tools:

    traceback()        # See where error occurred
    debug(function)    # Step through function
  2. Break down complex operations:

    # Instead of chaining everything
    result <- data %>% filter(...) %>% mutate(...) %>% summarise(...)
    
    # Do step by step
    step1 <- filter(data, ...)
    step2 <- mutate(step1, ...)
    result <- summarise(step2, ...)
  3. Check intermediate results:

    # Print intermediate steps
    print(dim(data))
    head(data)
    summary(data)

10 Prevention Strategies

  • Always check data structure after reading files
  • Use meaningful variable names to avoid confusion
  • Comment your code to remember what you were doing
  • Save your work frequently in case R crashes
  • Use version control (Git) to track changes

Copyright 2023-2025, Ronald G. Thomas