Get Started →
Other Errors

Condition has length > 1

Matches: the condition has length > 1

WHAT THIS ERROR MEANS

An if() statement received a vector with multiple values instead of a single TRUE or FALSE. if() only works with single logical values.

HOW TO FIX IT

1. Use any() or all() to reduce a logical vector to a single value.

2. Use ifelse() for vectorized conditions.

3. Check which element you actually want to test.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
x <- c(1, 2, 3)
if (x > 2) print("big")
GOOD — CORRECT APPROACH
x <- c(1, 2, 3)
if (any(x > 2)) print("at least one big")

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED OTHER ERRORS