The logical vectors in R Language are the vectors whose elements are TRUE
, FALSE
, or NA
(Not Available). R language allows the easy manipulation of logical (or relational) quantities. The TRUE
and
values are often used to represent the conditions or Boolean expressions.FALSE
Table of Contents
In R, the reserved words TRUE
and FALSE
are often abbreviated as T and F, respectively. However, the T and F are not reserved words and hence can be overwritten by the user. Therefore, instead of T and F; it is better to use TRUE
and FALSE
.
Logical vectors in R can be created by:
- Direct assignment of
TRUE
andFALSE
values to the elements of a vector - By using conditions (use of logical or comparison operators) on elements of the vectors. (Operators in R Language)
- Using ifelse statement
Creating Logical Vectors in R Using Direct Assignment
v1 <- c(TRUE, FALSE, TRUE) print(v1) ## Output [1] TRUE FALSE TRUE
Creating Logical Vectors using Comparison Operators
x <- 5 y <- 10 v2 <- x > y print(v2) ## Output FALSE
data <- c(1, 2, 3, 4, 5) v3 <- data < 3 print(v3) ## Output [1] TRUE TRUE FALSE FALSE FALSE
Creating Logical Vectors using ifelse Statement
The ifelse
statement can also be used to create/generate logical vectors in R Language. For example,
data <- c(3, 4, 6, 8, 4, 4, 6, 10, -5) v4 <- ifelse(data > 5, TRUE, FALSE) print(v4) ## Output [1] FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE
From the above examples, the logical vectors are usually generated by conditions. The length of the logical vector will be the same as that of the vectors to which the condition is applied. Depending on the condition, the corresponding elements result in FALSE
if the element of the vectors does not meet the condition specified and TRUE
where it is.
Logical Operators
The following is the list of logical operators
Logical Operator | Short Description |
---|---|
< | Less than |
> | Greater than |
<= | Less than or Equal to |
>= | Greater than or Equal to |
== | Exactly Equal to |
!= | Not Equal to |
In addition to logical operators, the relational/logical operators are:
Operator | Short Description |
---|---|
& (and) | It takes two logical values and returns TRUE only if both values are TRUE themselves |
| (or) | It takes two logical values and returns TRUEÂ if just one value is TRUE . |
! (not) | It negates the logical value it’s used on |
Use of Logical Operators
Filtering Data
The logical vectors in R language are commonly used for filtering the data. For example,
data <- data.frame(x = c(1, 2, 3, 4, 5), y = c("a", "b", "c", "d", "e")) filtered_data <- data[data$x > 3, ]
Ordinary Arithmetic
Logical vectors may be used in ordinary arithmetic, in which case they are coerced into numeric vectors, FALSE
becoming 0 and TRUE
becoming. For example,
x = c(TRUE, FALSE, FALSE, TRUE) y = c(5, 10, 6, 15) x+y ## Output [1] 6 10 6 16 sum(x) ## Output [1] 2
Logical vectors in R language are a fundamental tool for working with conditions and Boolean expressions. Understanding how to create, manipulate, and use logical vectors is essential for effective data analysis and programming in R.
https://itfeature.com, https://gmstat.com