HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
R›Conditions (if / else)

Conditions (if / else)

Making decisions with if-else if-else; conditions in parentheses, blocks in curly braces.

Simple enough for a 5-year-old to understand

decision with if

Example code
1
age <- 20
2
if (age >= 18) {
The condition is inside parentheses.
ConditionThe condition is inside parentheses.
3
cat("Adult\n", sep="")
4
} else {
5
cat("Child\n", sep="")
6
}
Condition
💡 Hint: else should be on the same line as the closing }.

About this topic

Conditions in R are written with if, else if, and else. The conditional expression must produce a single true-false value; considering R's vector structure, this is a rule that causes issues more often than expected.

If you want to make a separate decision for each element of the vector, instead of if, the vectorized ifelse function is used. Confusing the two is a very common mistake in R and produces a clear error message in newer versions.

Missing values in conditions require special attention. A comparison made with NA returns NA, and the condition cannot interpret this as true or false. When working with data, it is necessary to handle missing values before the condition.

What you will learn

  • the if, else if, and else structure
  • The requirement for the condition to be a single value
  • The vectorized condition function
  • The effect of missing values in conditions
  • Using switch

Who is this for?

  • R users learning flow control
  • Those who confuse vector and scalar conditions
  • Analysts working with missing data

When to use?

if is used when branching is needed based on a single value: checks like whether the file exists or if the parameter is valid.

A vectorized condition is used when it is necessary to produce a different value for each row of a column; this is both correct and fast.

Common mistakes

Wrong

v <- c(1, 5, 10)
if (v > 3) {
  print("greater")
}

Since the condition produced a three-element logical vector, the error "the condition has length > 1" is thrown in R 4.2 and later; in older versions, only the first element is used and the result is silently misleading. If you want to make a decision for each element, a vectorized function is needed.

Correct

v <- c(1, 5, 10)
sonuc <- ifelse(v > 3, "greater", "smaller")
print(sonuc)

Wrong

x <- 10
if (x = 10) print("equal")

A single equals sign is for assignment, a double equals sign is for comparison; this code will produce a syntax error. Since R does not allow assignment inside a condition, it shows the error immediately — in some languages, this error runs silently and the condition is always true.

Correct

x <- 10
if (x == 10) print("equal")

Wrong

x <- NA
if (x > 5) print("greater")

A comparison made with a missing value returns NA and the condition cannot reach a decision; the error "missing value where TRUE/FALSE needed" is thrown. This situation is frequently encountered in datasets, so it is necessary to check for missingness before the condition.

Correct

x <- NA
if (!is.na(x) && x > 5) print("greater") else print("unknown")

Frequently asked questions

When is ifelse used?

When it is necessary to make a separate decision for each element of a vector and produce a result of the same length. For a single value, if is sufficient and more readable; using ifelse for a single value creates unnecessary complexity.

In which case is switch preferred?

When branching based on a value being equal to one of several fixed options. It is more readable compared to long else if chains. R's switch function works very well with text values and a default branch can also be defined.

What do any and all do?

any returns true if there is at least one true in the logical vector; all returns true if all of them are true. These are the correct ways to reduce a vector condition to a single value and can be safely used inside if.

Summary

The if condition must be a single true-false value.

ifelse is used for vector conditions.

A missing value in a condition produces an error.

any and all reduce a vector to a single value.

Assignment and comparison should not be confused.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code