How to Perform Element-Wise Comparisons in R


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,

  1. We start by creating a matrix named mat.
  2. We compare each element of the matrix mat with a scalar value 3 using the greater than operator >.
  3. This comparison returns a matrix of logical values where each element indicates whether the corresponding element in mat is greater than 3.
  4. We assign the result to a variable named comparison_result.
  5. 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,

  1. We start by creating two matrices named mat1 and mat2.
  2. We compare each element of mat1 with the corresponding element in mat2 using the equality operator ==.
  3. This comparison returns a matrix of logical values where each element indicates whether the corresponding elements in mat1 and mat2 are equal.
  4. We assign the result to a variable named comparison_result.
  5. 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,

  1. We start by creating a matrix named mat.
  2. We compare each element of the matrix mat with a scalar value 4 using the less than or equal to operator <=.
  3. This comparison returns a matrix of logical values where each element indicates whether the corresponding element in mat is less than or equal to 4.
  4. We assign the result to a variable named comparison_result.
  5. 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

  1. How to Create a Matrix in R ?
  2. How to Combine Matrices by Columns in R ?
  3. How to Combine Matrices by Rows in R ?
  4. How to Create Diagonal Matrices in R ?
  5. How to Access Matrix Elements using Indexing and Slicing in R ?
  6. How to get Matrix Size in R ?
  7. How to get Number of Rows in Matrix in R ?
  8. How to get Number of Columns in Matrix in R ?
  9. How to do Matrix Addition in R ?
  10. How to do Matrix Subtraction in R ?
  11. How to do Matrix Multiplication in R ?
  12. How to do Scalar Multiplication on a Matrix in R ?
  13. How to do Scalar Division on a Matrix in R ?
  14. How to do Element-Wise Operations in a Matrix in R ?
  15. How to Assign Row and Column Names in a Matrix in R ?
  16. How to get Row Names in a Matrix in R ?
  17. How to get Column Names in a Matrix in R ?
  18. How to find Transpose of a Matrix in R ?
  19. How to Extract the Diagonal of a Matrix in R ?
  20. How to find Determinant of a Matrix in R ?
  21. How to find Inverse of a Matrix in R ?
  22. How to find Rank of a Matrix in R ?
  23. How to Find Eigenvalues and Eigenvectors of a Matrix in R ?
  24. How to Perform Singular Value Decomposition (SVD) in R ?
  25. How to Perform QR Decomposition in R ?
  26. How to Perform Cholesky Decomposition in R ?
  27. How to Reshape Matrices in R ?
  28. How to Convert Data Frames to Matrices in R ?
  29. How to Create Identity Matrix in R ?
  30. How to Create Zero Matrix in R ?
  31. How to Create Ones Matrix in R ?
  32. How to Generate Random Matrices in R ?
  33. How to Calculate Row and Column Sums in R ?
  34. How to Calculate Row and Column Means in R ?
  35. How to Find Row and Column Max/Min in R ?
  36. How to Perform Element-Wise Comparisons in R ?