0
0
R Programmingprogramming~5 mins

Logical (boolean) type in R Programming

Choose your learning style9 modes available
Introduction

The logical type in R is used to represent true or false values. It helps you make decisions in your code.

Checking if a number is greater than another number.
Deciding whether to run a part of your program based on a condition.
Filtering data to keep only rows that meet a condition.
Storing answers to yes/no questions in your program.
Syntax
R Programming
variable <- TRUE
variable <- FALSE

Logical values in R are written as TRUE or FALSE (all uppercase).

You can also use T and F as shortcuts, but it's safer to use the full words.

Examples
This creates a logical variable x with the value TRUE and prints it.
R Programming
x <- TRUE
print(x)
This creates a logical variable y with the value FALSE and prints it.
R Programming
y <- FALSE
print(y)
This checks if 5 is greater than 3, which is TRUE, and stores it in z.
R Programming
z <- 5 > 3
print(z)
Sample Program

This program creates three logical variables and prints their values.

R Programming
a <- TRUE
b <- FALSE
c <- 10 == 10
print(a)
print(b)
print(c)
OutputSuccess
Important Notes

Logical values are useful in if statements and loops to control the flow of your program.

When you print logical values, R shows them as TRUE or FALSE with an index like [1].

Summary

Logical type stores TRUE or FALSE values.

Use logical values to make decisions in your code.

Logical expressions like comparisons return logical values.