Boolean Operations and Logical Functions in R Programming
- Bernard Kilonzo

- Aug 20
- 3 min read

Overview
Boolean operations are fundamental tools in Boolean algebra where variables represent truth values - true or false. The main operations include AND, which yields true only if both inputs are true; OR, which yields true if at least one input is true; and NOT, which inverts the truth value. These operations are essential for evaluating logical expressions and form the foundation of digital logic circuits and programming.
Logical functions apply these Boolean operations to inputs to produce a Boolean output. Common logical functions such as IF, AND, OR, and NOT are widely used in programming, spreadsheets, and algorithms to enable decision-making based on conditions. By evaluating expressions to true or false, logical functions help control program flow and implement conditional logic in practical computing environments.
Examples of Boolean Operations and Logical Functions
Loading the required package and data set
library(tidyverse)
superstore<-read.csv("https://raw.githubusercontent.com/bernardkilonzo-rigor/dataviz/main/data/Sample%20-%20Superstore.csv")1. CASE Function
The CASE function in programming is a control structure used to execute different blocks of code based on the value of a variable or expression. It's like a more elegant alternative to a long chain of if-else statements, especially when you're comparing a single value against multiple possibilities.
Example
superstore<-superstore%>%
mutate(class =case_when(
Profit>0 ~ "Profitable",
Profit<0 ~ "Unprofitable",
TRUE ~ "Zero"))(The above code creates a new column “class”, categorizing Profit as either Profitable, Unprofitable, or Zero)
2. IF_ELSE
The if-else function in programming is a fundamental control structure that lets your code make decisions. It checks whether a condition is true or false, and then executes different blocks of code depending on the result.
Example
superstore<-superstore%>%
mutate(class_cal =
if_else(Profit >= 1000, "Super Profitable",
if_else(Profit >= 150, "Highly Profitable",
if_else(Profit >= 0, "Profitable",
if_else(is.na(Profit), "Missing","Unprofitable")))))(The above code creates a new column “class_cal”, categorizing Profit as either Unprofitable, Profitable, Highly Profitable, and Super Profitable)
3. IF & AND
The IF AND function in programming is a way to combine multiple conditions and make decisions based on whether all of them are true. It’s like saying: “Do this, but only if this AND that are both true.”
Example
superstore<-superstore%>%
mutate(if_and =
if_else(class == "Profitable" & Sales > 1000, "Green Light",
if_else(class == "Unprofitable" & Sales > 1000, "Red Light",
if_else(class == "Zero" & Sales >1000, "Yellow Light", "Others"))))(The above code creates a new column “if_and”, which categorizes the orders as either Green Light, Red Light, Yellow Light, or Others based on the Sales and Profit values)
4. IF & OR
The IF OR function in programming is used when you want to execute a block of code if at least one of multiple conditions is true. It’s like saying: “Do this if this OR that is true.”
Example
superstore<-superstore%>%
mutate(if_or =
if_else(Profit > 1000 | Sales > 1000, "High Value","Others"))(The above code creates a new column “if_or”, which categorizes the orders as either High Value or Others based on the Sales or Profit values)
5. IN (%in%)
The IN function (or operator) in programming is used to check whether a value exists within a set, list, array, or range. It's a concise way to test membership - like asking, “Is this item in the group?”
Example
superstore<-superstore%>%
mutate(New_Region =
if_else(Region %in% c("South","Central"), "Coastal Region",Region))(The above code creates a new column “New_Region”, by assigning a new name “Coastal Region” whenever the name “South” or “Central” is found on the Region column otherwise the default name is returned)
The screen shot below shows the new columns generated using the above Boolean operations and logical functions.

Check-out the entire code from GitHub
Conclusion
Boolean operators and logical functions are the backbone of decision-making in R programming. They empower developers and data analysts to construct precise, condition-based logic that drives everything from simple comparisons to complex data transformations. With core Boolean operators like &, |, and !, users can evaluate relationships between logical expressions, while functions such as ifelse() provide a powerful, vectorized way to apply conditional logic across entire datasets.
Understanding and mastering these tools is essential for writing clean, efficient, and readable R code. Whether you're filtering data, building control structures, or designing algorithms, Boolean logic enables you to express intent clearly and handle diverse scenarios with confidence. As you continue working with R, integrating Boolean operations and logical functions into your workflow will not only enhance your analytical capabilities but also deepen your understanding of how data-driven decisions are made programmatically.
If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!
Thank you for reading!!
