R - Return Position of Element in Matrix

R - return position of element in matrix?

Here is one approach

mat = matrix(rnorm(9), 3, 3)
which(mat !=0, arr.ind = T)

Return position of specific element in matrix - R

The question doesn't say how this matrix has been constructed, but this problem seems to arise from 0.8579698 being the truncated expression of a real (float) value. In general, you can't use exact equality for real values:

> .72==.72
[1] TRUE

But:

> sqrt(.72)
[1] 0.8485281
> sqrt(.72)==0.8485281
[1] FALSE

There is a small difference between those apparently equal numbers:

> sqrt(.72)-0.8485281
[1] 3.742386e-08

A common workaround is to use a difference threshold instead of an equality:

> m<-matrix(c(1,1,.72,1,1,.72,.72,.72,1),nrow=3,ncol=3)
> (m<-sqrt(m))
[,1] [,2] [,3]
[1,] 1.0000000 1.0000000 0.8485281
[2,] 1.0000000 1.0000000 0.8485281
[3,] 0.8485281 0.8485281 1.0000000
> which(abs(m-.8485)<.0001,arr.ind = TRUE)
row col
[1,] 3 1
[2,] 3 2
[3,] 1 3
[4,] 2 3

Is there an R function for finding the index of an element in a vector?

The function match works on vectors:

x <- sample(1:10)
x
# [1] 4 5 9 3 8 1 6 10 7 2
match(c(4,8),x)
# [1] 1 5

match only returns the first encounter of a match, as you requested. It returns the position in the second argument of the values in the first argument.

For multiple matching, %in% is the way to go:

x <- sample(1:4,10,replace=TRUE)
x
# [1] 3 4 3 3 2 3 1 1 2 2
which(x %in% c(2,4))
# [1] 2 5 9 10

%in% returns a logical vector as long as the first argument, with a TRUE if that value can be found in the second argument and a FALSE otherwise.

How to get the index of elements in a matrix that match values of a vector

Here's a function in base R to do this -

match_a_row <- function(data, var1, var2) {
which(data[[1]] == var1 & data[[2]] == var2)
}

match_a_row(m, 'exponential', 'logit')
#[1] 2
match_a_row(m, 'independent', 'probit')
#[1] 8

Find array index of elements of a matrix that match a value in a vector of candidate values

MrFlick's solution is probably one of the best you'll get but if you must stick with in-built functions in base R, here's one way -

which(matrix(m %in% candidates, dim(m)), arr.ind = T)

row col
[1,] 2 1
[2,] 3 1

Another way with lapply and Reduce but above should be faster -

which(Reduce("|", lapply(candidates, function(x) m == x)), arr.ind = T)

row col
[1,] 2 1
[2,] 3 1

Select element in matrix with index from other matrix

It may be due to rounding differences. This can happen when you stored a vlue into y[1,1] which was a result from a calculation.

Let me give you an example when this can happen.

mat <- matrix(1:10, ncol = 2)
print(mat)
# [,1] [,2]
# [1,] "a" "f"
# [2,] "b" "g"
# [3,] "c" "h"
# [4,] "d" "i"
# [5,] "e" "j"

ind <- exp(4) / 54.598152 * 2
print(ind)
# [1] 2

print(mat[1, ind]) # Extracts the value from the first column.
# [1] "a"

print(mat[1, round(ind)]) # Extracts the value from the second column.
# [1] "f"

Therefore, try using z[1, round(y[1,1])] to check if the problem persists.

Index value for matrix in R?

Just looked at the help for which() after posting this and found the answer: the arr.ind parameter.

which(a==23, arr.ind=TRUE)
row col
[1,] 3 5

Indexing elements in matrix and corresponding column numbers

We can do

setNames(lapply(unique(m1), function(i) 
as.vector(which(m1==i, arr.ind = TRUE)[,2])), unique(m1))

Or another option is

split(col(m1), m1)

data

m1 <- structure(c(31738, 1687741813, 44, 3136023010, 44, 123, 777150982, 
31738, 123, 2318301701, 1284682632, 31738, 44, 462137835, 1215490197,
3707934113, 445275140, 123), .Dim = c(3L, 6L))


Related Topics



Leave a reply



Submit