You are currently viewing R ifelse() Usage with Examples

R ifelse() function is a vectorized version of an if … else statement. It returns an object in the same shape as the object you used in the test condition. For example, if you use a vector in a condition, it returns a vector of the same size with values filled based on the condition result for each element.

In R Programming, most of the functions take a vector as input and return a vectorized output. so, ifelse() is a function that takes a vector as a test condition and executes the test condition for each element in the vector.

1. Quick Examples of ifelse() Function

Following are quick examples of using the ifelse() function in R


# Simple ifelse() example
gender <- 'F'
result <- ifelse(gender == 'M', 'MALE', "FEMALE")
print(result)

# Using ifelse() statement with multiple conditions
a=50
val = ifelse(a >= 40 & b <= 60, 'TRUE', "FALSE")
print(val)

# With same vector length
x <- c(6,5,4,3,2)
y <- c(2,3,4,5,6)
res <- ifelse(x > y,x,y)
print(res)

# With different vector length
x < c(4)
y <- c(2,3,4,5,6)
res <- ifelse(x > y,x,y)
print(res)

# Using with DataFrame
# Refer below sections for complete example
df$size <- ifelse(df$chapters > 0 & df$chapters < 20, 'SMALL','BIG')

2. R ifelse() Syntax

The syntax of the ifelse() function is as follows.


# Syntax of ifelse() function
ifelse(test, yes, no)

Parameters

  • test – Test condition
  • yes – Value to be returned when test condition evaluates to TRUE.
  • no – Value to be returned when test condition evaluates to FALSE.

3. R ifelse() with Vector

R ifelse() function shines over if…else when you use it with Vector to evaluate test conditions for each element. For example, you have a vector of values and you want to test these values against another vector element by element.

Here, it tests the condition element by element from the vector and fills the yes or no values in the resultant vector.


# With same vector length
x <- c(6,5,4,3,2)
y <- c(2,3,4,5,6)
res <- ifelse(x > y,x,y)
print(res)

# Output
# [1] 6 5 4 5 6

Another example of ifelse() with different vector lengths.


# With different vector length
x < c(4)
y <- c(2,3,4,5,6)
res <- ifelse(x > y,x,y)
print(res)

# Output
# [1] 4 4 4 5 6

4. R ifelse() With Simple Value

Let’s see a simple example ifelse() using a string datatype.


# Simple ifelse() example
gender <- 'F'
result <- ifelse(gender == 'M', 'MALE', "FEMALE")
print(result)

# Output
#[1] "MALE"

5. ifelse() with Multiple Conditions

We can use ifelse() similarly to if…else with multiple conditions. To combine the conditions you can use logical operators & (AND), | (OR).


# Using ifelse() statement
a=50
val = ifelse(a >= 40 & b <= 60, 'TRUE', "FALSE")
print(val)

# Output
#[1] "TRUE"

6. ifelse() with DataFrame

The following example shows how to add a new column size to the DataFrame by using if else with multiple conditions in R. Here, we use dollar ($) in R to refer to DataFrame columns.


# Create dataframe
df=data.frame(id=c(11,22,33,44,55),
              pages=c(32,45,33,22,56),
              name=c("spark","python","R","java","jsp"),
              chapters=c(76,86,11,15,7),
              price=c(144,553,321,567,890))

#display the dataframe
print(df)

# Using multiple conditions
df$size <- ifelse(df$chapters > 0 & df$chapters < 20, 'SMALL','BIG')

Yields below output.

r ifelse

Conclusion

In this article, you have learned how to use R ifelse() statement with examples. R ifelse() function is a vectorized version of an if … else statement. It returns an object in the same shape as the object you used in the test condition.

Related Articles