given value of matrix, getting it's coordinate
Categories:
Locating Values in a Matrix: Retrieving Coordinates in R

Learn how to efficiently find the row and column indices (coordinates) of specific values within a matrix in R, using various practical methods.
Working with matrices is a fundamental aspect of data manipulation in R. Often, you might need to not just check for the presence of a value, but also pinpoint its exact location within the matrix. This article will guide you through several robust methods to retrieve the coordinates (row and column indices) of one or more specific values in an R matrix. We'll cover approaches using base R functions, logical indexing, and the which()
function, providing clear examples for each.
Understanding Matrix Coordinates
In R, a matrix is a two-dimensional data structure where elements are arranged in rows and columns. Each element can be uniquely identified by its row index and column index. For example, in a matrix M
, M[i, j]
refers to the element in the i
-th row and j
-th column. When we talk about 'coordinates' of a value, we are referring to these [row, column]
pairs.
flowchart TD A[Start with a Matrix] --> B{Identify Target Value(s)} B --> C{Apply Search Method} C --> D["Method 1: `which()` with `arr.ind = TRUE`"] C --> E["Method 2: Logical Indexing + `which()`"] C --> F["Method 3: `apply()` (less common for this task)"] D --> G[Get Row/Column Indices] E --> G F --> G G --> H[Output Coordinates] H --> I[End]
Workflow for finding matrix coordinates in R.
Method 1: Using which()
with arr.ind = TRUE
The which()
function in R is incredibly versatile. When applied to a logical matrix (a matrix of TRUE
/FALSE
values), it returns the linear indices of the TRUE
values. However, by setting the arr.ind
argument to TRUE
, which()
will return a matrix where each row represents the [row, column]
coordinates of the TRUE
values. This is often the most straightforward and recommended method.
# Create a sample matrix
my_matrix <- matrix(c(1, 5, 3, 8, 2, 5, 9, 4, 7), nrow = 3, byrow = TRUE)
print(my_matrix)
# Find coordinates of the value 5
coords_of_5 <- which(my_matrix == 5, arr.ind = TRUE)
print(coords_of_5)
# Find coordinates of values greater than 6
coords_gt_6 <- which(my_matrix > 6, arr.ind = TRUE)
print(coords_gt_6)
Using which(..., arr.ind = TRUE)
to find coordinates.
arr.ind = TRUE
argument is crucial for getting row and column indices directly. Without it, which()
would return linear indices, which then require further calculation to convert to 2D coordinates.Method 2: Logical Indexing and which()
(for specific rows/columns)
While arr.ind = TRUE
is generally preferred, you might sometimes want to find coordinates by first identifying rows and columns separately, especially if you're performing more complex conditional checks or need to process row/column indices independently. This method involves creating a logical matrix and then using which()
on its rows and columns.
# Create a sample matrix
my_matrix <- matrix(c(10, 20, 30, 40, 50, 60, 70, 80, 90), nrow = 3, byrow = TRUE)
print(my_matrix)
# Find values equal to 50
logical_matrix <- (my_matrix == 50)
print(logical_matrix)
# Get row indices where value is 50
row_indices <- which(apply(logical_matrix, 1, any))
print(row_indices)
# Get column indices where value is 50
col_indices <- which(apply(logical_matrix, 2, any))
print(col_indices)
# To get combined coordinates, you'd typically still use which(..., arr.ind=TRUE)
# This method is more for identifying *which rows/columns* contain the value, not its exact cell.
Using logical indexing and apply()
to find rows/columns containing a value.
[row, col]
pairs for each instance of a value. It's more useful for determining which entire rows or columns contain at least one instance of the target value, rather than the precise location of every instance.