Index Values from a Matrix Using Row, Col Indices

Index values from a matrix using row, col indices

Almost. Needs to be offered to "[" as a two column matrix:

dat$matval <- mat[ cbind(dat$I, dat$J) ] # should do it.

There is a caveat: Although this also works for dataframes, they are first coerced to matrix-class and if any are non-numeric, the entire matrix becomes the "lowest denominator" class.

Index a matrix using two equal-length vectors for row and column indices

You can use cbind(R, C) for indexing with [:

m[cbind(R,C)]

#> [1] 6 18 20

Use row and columns indices in matrix to extract values from matrix

From ?"[", you will find the following:

When indexing arrays by [ a single argument i can be a matrix with as many columns as there are dimensions of x; the result is then a vector with elements corresponding to the sets of indices in each row of i.

and later on...

A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector. Negative indices are not allowed in the index matrix. NA and zero values are allowed: rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.

Thus, what you are looking for is simply:

A[v]

Extract values from matrix using col row indices

You could try

t(sapply(seq_len(nrow(a)), function(i) a[i, b[i, ]]))
# [,1] [,2] [,3]
# [1,] 7 6 7
# [2,] 7 3 7
# [3,] 10 6 10

And you may see a slight speed improvement from the sapply solution above with vapply

s <- seq_len(nrow(a))
t(vapply(s, function(i) a[i, b[i, ]], numeric(ncol(b))))
# [,1] [,2] [,3]
# [1,] 7 6 7
# [2,] 7 3 7
# [3,] 10 6 10

Or a for loop solution is

m <- matrix(, nrow(b), ncol(b))
for(i in seq_len(nrow(a))) { m[i, ] <- a[i, b[i, ]] }
m
# [,1] [,2] [,3]
# [1,] 7 6 7
# [2,] 7 3 7
# [3,] 10 6 10

How to extract specific values from Dataframe or Matrix using vectors of row and column indices?

We can cbind the indices to extract the values

mat[cbind(rowind, colind)]
#[1] 11 12 1

Extract values from matrix based on a matrix of row indices and matrix of column indices

array(mat[cbind(c(row_indices), c(col_indices))], dim(row_indices))

[,1] [,2]
[1,] 1 12
[2,] 18 10

assign new matrix values based on row and column index vectors

sub2ind can help here,

A = zeros(3,2)
rows = [1 2 3];
cols = [1 2 1];

A(sub2ind(size(A),rows,cols))=1
A =

1 0
0 1
1 0

with a vector to 'insert'

b = [1,2,3];
A(sub2ind(size(A),rows,cols))=b

A =

1 0
0 2
3 0

Is there a way to use columns in a dataframe to index a matrix to produce another column via mutation function?

Try

df2 %>%
mutate(new_col = matrix_1[cbind(col_1, col_2)])

[.matrix (and [.data.frame) allows you to index by a set of row/column indices by using a matrix. Namely, from ?[:

i, j, ...: indices specifying elements to extract or replace.  Indices
...

When indexing arrays by '[' a single argument 'i' can be a
matrix with as many columns as there are dimensions of 'x';
the result is then a vector with elements corresponding to
the sets of indices in each row of 'i'.

Here's a reprex:

m <- matrix(1:12, nrow = 3)
m
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12

df <- data.frame(col1 = c(1, 3), col2 = c(2, 4))
df
# col1 col2
# 1 1 2
# 2 3 4

library(dplyr)
df %>%
mutate(newcol = m[cbind(col1, col2)])
# col1 col2 newcol
# 1 1 2 4
# 2 3 4 12


Related Topics



Leave a reply



Submit