R Error: non-numeric argument to binary operator — Type Mismatch Fix

Error in x + y : non-numeric argument to binary operator means you tried to use an arithmetic operator (+, -, *, /) on something that is not a number — usually a character string or a factor.

The Error

# Reproduce the error: x <- "5" y <- 3 # x + y # Error: non-numeric argument to binary operator cat("class of x:", class(x), "\n") cat("class of y:", class(y), "\n") cat("R cannot add a character '5' to a numeric 3.\n")


  

Cause 1: Character String That Looks Like a Number

Data read from CSV files or user input often arrives as character strings, even if the values look numeric:

# Simulating data read from a file revenue <- c("1000", "2500", "3200") cat("Class:", class(revenue), "\n") # This fails: # total <- sum(revenue) # Error # Fix: convert to numeric first revenue_num <- as.numeric(revenue) cat("Class after conversion:", class(revenue_num), "\n") cat("Sum:", sum(revenue_num), "\n")


  

Fix: Use as.numeric() to convert character strings to numbers before doing math.

Cause 2: Factor Used in Arithmetic

Factors store category labels. Using them in math gives unexpected results or errors:

df <- data.frame(score = factor(c(10, 20, 30))) cat("Class:", class(df$score), "\n") cat("Levels:", levels(df$score), "\n") # Wrong way to convert factor to numeric: wrong <- as.numeric(df$score) # Gives 1, 2, 3 (level indices!) cat("Wrong (level indices):", wrong, "\n") # Correct way: convert factor -> character -> numeric right <- as.numeric(as.character(df$score)) cat("Correct (actual values):", right, "\n") cat("Sum:", sum(right), "\n")


  

Fix: Convert factors to numeric with as.numeric(as.character(x)). Never use as.numeric() directly on a factor — it returns level indices.

Cause 3: Hidden Non-Numeric Values in Data

A column may contain mostly numbers but have a few non-numeric entries (like "N/A", "$100", or "TBD"):

prices <- c("25.99", "30.00", "$42.50", "N/A", "18.75") cat("Original:", prices, "\n") # as.numeric will produce NAs for non-numeric values converted <- suppressWarnings(as.numeric(prices)) cat("Converted:", converted, "\n") cat("NAs introduced:", sum(is.na(converted)), "\n") # Fix: clean the strings first cleaned <- gsub("[$,]", "", prices) # Remove $ and commas cleaned[cleaned == "N/A"] <- NA # Handle N/A result <- as.numeric(cleaned) cat("Cleaned:", result, "\n") cat("Sum (ignoring NA):", sum(result, na.rm = TRUE), "\n")


  

Fix: Clean strings with gsub() to remove currency symbols, commas, or other non-numeric characters before converting.

Cause 4: Wrong Column Selected from Data Frame

You accidentally reference the wrong column or get a data frame instead of a vector:

df <- data.frame(name = c("Alice", "Bob"), score = c(85, 92)) # Accidentally using the name column in math: # df$name * 2 # Error: non-numeric argument # Check your column types first cat("Column types:\n") str(df) # Fix: use the correct numeric column cat("\nMean score:", mean(df$score), "\n")


  

Fix: Use str(df) to check column types. Make sure you reference the numeric column.

Cause 5: Pasting Instead of Adding

A common beginner mistake is using + to concatenate strings (works in Python, not R):

first <- "Hello" last <- "World" # Wrong (R uses + for math only): # first + last # Error: non-numeric argument # Fix: use paste() for string concatenation result <- paste(first, last) cat(result, "\n") # Or paste0() for no separator cat(paste0(first, last), "\n")


  

Fix: Use paste() or paste0() for string concatenation, not +.

Practice Exercise

# Exercise: This data frame has type problems. Fix all columns # so you can compute total_cost = price * quantity. orders <- data.frame( item = c("Widget", "Gadget", "Doohickey"), price = c("$12.50", "$8.00", "$15.75"), quantity = factor(c(3, 5, 2)), stringsAsFactors = FALSE ) # Write code to clean the data and compute total_cost:


  
Click to reveal solution ```r
orders <- data.frame( item = c("Widget", "Gadget", "Doohickey"), price = c("$12.50", "$8.00", "$15.75"), quantity = factor(c(3, 5, 2)), stringsAsFactors = FALSE ) # Fix price: remove $ sign, convert to numeric orders$price <- as.numeric(gsub("\\$", "", orders$price)) # Fix quantity: factor -> character -> numeric orders$quantity <- as.numeric(as.character(orders$quantity)) # Now compute total_cost orders$total_cost <- orders$price * orders$quantity print(orders) cat("Grand total:", sum(orders$total_cost), "\n")

  
**Explanation:** The price column is character with dollar signs (needs `gsub` + `as.numeric`). The quantity column is a factor (needs `as.character` first, then `as.numeric`). After cleaning both, multiplication works.

Summary

Cause Fix Prevention
Character looks like number as.numeric(x) Check class() after reading data
Factor in arithmetic as.numeric(as.character(x)) Use stringsAsFactors = FALSE
Non-numeric characters ($, commas) gsub() then as.numeric() Clean data on import
Wrong column selected Use str(df) to verify Check column names and types first
String concatenation with + Use paste() or paste0() Remember: + is only for math in R

FAQ

Why does as.numeric() on a factor give wrong numbers?

as.numeric(factor(c(10, 20, 30))) returns 1, 2, 3 — the internal level indices, not the label values. R stores factors as integers internally. To get the actual numbers, go through character first: as.numeric(as.character(x)).

How can I find which values in a column are non-numeric?

Use is.na(suppressWarnings(as.numeric(x))) to find positions where conversion fails. For example: x[is.na(suppressWarnings(as.numeric(x)))] shows the problematic values.

What's Next?

  1. R Warning: NAs introduced by coercion — what happens when as.numeric() fails
  2. R Error: object 'x' not found — variable not found troubleshooting
  3. R Common Errors — the full reference of 50 common errors