How to Perform Element-Wise Comparisons in R - Step by Step Examples
How to Perform Element-Wise Comparisons in R ?
Answer
In R, you can perform element-wise comparisons on matrices using logical operators such as ==
, !=
, <
, <=
, >
, and >=
. These comparisons return a matrix of the same dimensions with logical values indicating the result of the comparison for each element.
✐ Examples
1 Comparing Elements of a Matrix with a Scalar
In this example,
- We start by creating a matrix named
mat
. - We compare each element of the matrix
mat
with a scalar value3
using the greater than operator>
. - This comparison returns a matrix of logical values where each element indicates whether the corresponding element in
mat
is greater than3
. - We assign the result to a variable named
comparison_result
. - We print the
comparison_result
to the console to see the matrix of logical values.
R Program
mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
comparison_result <- mat > 3
print(comparison_result)
Output
[,1] [,2] [,3] [1,] FALSE FALSE FALSE [2,] TRUE TRUE TRUE
2 Comparing Two Matrices Element-Wise
In this example,
- We start by creating two matrices named
mat1
andmat2
. - We compare each element of
mat1
with the corresponding element inmat2
using the equality operator==
. - This comparison returns a matrix of logical values where each element indicates whether the corresponding elements in
mat1
andmat2
are equal. - We assign the result to a variable named
comparison_result
. - We print the
comparison_result
to the console to see the matrix of logical values.
R Program
mat1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
mat2 <- matrix(c(1, 1, 3, 4, 4, 6), nrow = 2)
comparison_result <- mat1 == mat2
print(comparison_result)
Output
[,1] [,2] [,3] [1,] TRUE FALSE TRUE [2,] TRUE FALSE TRUE
3 Finding Elements Less Than or Equal to a Value
In this example,
- We start by creating a matrix named
mat
. - We compare each element of the matrix
mat
with a scalar value4
using the less than or equal to operator<=
. - This comparison returns a matrix of logical values where each element indicates whether the corresponding element in
mat
is less than or equal to4
. - We assign the result to a variable named
comparison_result
. - We print the
comparison_result
to the console to see the matrix of logical values.
R Program
mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
comparison_result <- mat <= 4
print(comparison_result)
Output
[,1] [,2] [,3] [1,] TRUE TRUE TRUE [2,] TRUE FALSE FALSE
Summary
In this tutorial, we learned How to Perform Element-Wise Comparisons in R language with well detailed examples.
More R Matrices Tutorials
- How to Create a Matrix in R ?
- How to Combine Matrices by Columns in R ?
- How to Combine Matrices by Rows in R ?
- How to Create Diagonal Matrices in R ?
- How to Access Matrix Elements using Indexing and Slicing in R ?
- How to get Matrix Size in R ?
- How to get Number of Rows in Matrix in R ?
- How to get Number of Columns in Matrix in R ?
- How to do Matrix Addition in R ?
- How to do Matrix Subtraction in R ?
- How to do Matrix Multiplication in R ?
- How to do Scalar Multiplication on a Matrix in R ?
- How to do Scalar Division on a Matrix in R ?
- How to do Element-Wise Operations in a Matrix in R ?
- How to Assign Row and Column Names in a Matrix in R ?
- How to get Row Names in a Matrix in R ?
- How to get Column Names in a Matrix in R ?
- How to find Transpose of a Matrix in R ?
- How to Extract the Diagonal of a Matrix in R ?
- How to find Determinant of a Matrix in R ?
- How to find Inverse of a Matrix in R ?
- How to find Rank of a Matrix in R ?
- How to Find Eigenvalues and Eigenvectors of a Matrix in R ?
- How to Perform Singular Value Decomposition (SVD) in R ?
- How to Perform QR Decomposition in R ?
- How to Perform Cholesky Decomposition in R ?
- How to Reshape Matrices in R ?
- How to Convert Data Frames to Matrices in R ?
- How to Create Identity Matrix in R ?
- How to Create Zero Matrix in R ?
- How to Create Ones Matrix in R ?
- How to Generate Random Matrices in R ?
- How to Calculate Row and Column Sums in R ?
- How to Calculate Row and Column Means in R ?
- How to Find Row and Column Max/Min in R ?
- How to Perform Element-Wise Comparisons in R ?