0
0
R Programmingprogramming~10 mins

Ifelse vectorized function in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Ifelse vectorized function
Start with vectors
Evaluate condition element-wise
For each element:
Pick true
Combine results
Return new vector
The ifelse function checks each element of a condition vector and picks values from two vectors accordingly, returning a new vector.
Execution Sample
R Programming
x <- c(3, 7, 2, 9)
ifelse(x > 5, "big", "small")
This code labels each number in x as 'big' if greater than 5, otherwise 'small'.
Execution Table
Indexx[i]Condition (x[i] > 5)Value if TRUEValue if FALSEResult[i]
13FALSE"big""small""small"
27TRUE"big""small""big"
32FALSE"big""small""small"
49TRUE"big""small""big"
💡 All elements processed, result vector created with values chosen based on condition.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
xc(3,7,2,9)3729c(3,7,2,9)
ConditionNAFALSETRUEFALSETRUEc(FALSE, TRUE, FALSE, TRUE)
ResultNA"small""big""small""big"c("small", "big", "small", "big")
Key Moments - 2 Insights
Why does ifelse return a vector instead of a single value?
Because ifelse checks the condition for each element of the input vector separately and returns a vector of results, as shown in the execution_table where each index produces one output.
What happens if the condition vector is shorter than the true/false vectors?
R adjusts the true/false vectors to match the condition vector's length using rep_len(): recycling if shorter, truncating if longer. In this example, "big" and "small" (length 1) are recycled to length 4 (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result for x[3]?
A"small"
B"big"
CFALSE
DTRUE
💡 Hint
Check row with Index 3 in execution_table under Result[i]
At which index does the condition x[i] > 5 become TRUE for the first time?
A1
B2
C3
D4
💡 Hint
Look at the Condition column in execution_table and find first TRUE
If x was c(6, 4, 8, 1), what would be the result at index 2?
A"big"
BTRUE
C"small"
DFALSE
💡 Hint
Compare new x[2] = 4 with condition x[i] > 5, see execution_table logic
Concept Snapshot
ifelse(condition, true_value, false_value)
- Checks condition element-wise
- Returns vector picking true_value or false_value per element
- Vectorized: fast and concise
- Useful for labeling or replacing values
- Condition, true_value, false_value recycle if needed
Full Transcript
The ifelse function in R takes a condition vector and two other vectors or values. It checks each element of the condition. If true, it picks the corresponding element from the true_value vector; if false, from the false_value vector. This happens for every element, producing a new vector of the same length. For example, with x = c(3,7,2,9), ifelse(x > 5, "big", "small") returns c("small", "big", "small", "big") because only 7 and 9 are greater than 5. This vectorized approach avoids loops and is efficient. The execution table shows each step clearly, and the variable tracker follows how values change. Remember, if vectors differ in length, R recycles shorter ones to match. This function is great for quick conditional replacements or labeling in data.