Get column index of maximum value in each Row of matrix - r

I have a 6 x 10 matrix where I have to find the row index and column index of the maximum value in each row.
set.seed(75)
amat <- matrix( sample(10, size=60, replace=T), nrow=6)
which gives me the matrix:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 6 7 7 2 4 3 7 1 4
[2,] 1 9 8 7 2 6 10 9 5 2
[3,] 7 10 8 4 10 5 4 8 4 4
[4,] 4 3 1 1 3 3 9 7 4 2
[5,] 1 8 1 9 9 8 1 3 7 7
[6,] 2 6 7 5 6 10 4 6 10 1
Now, I want to navigate row by row, and get the row index and column index of the maximum value in each row.
To get the maximum value in each row, I did:
apply(amat,1,max)
[1] 7 10 10 9 9 10
How do I get the row and column indices of the first occurrence of the maximum value?
Thanks

We can use max.col
cbind(1:nrow(amat), max.col(amat, 'first'))

Related

Create new vectors with no element being in the same position as the original vector as well as with other vectors?

This question is an extension of a question I asked earlier.
Suppose I have a vector V1 (with two or more elements):
V1 <- 1:10
I want to sample one or several vectors that:
(1). no element is in the same position as the original vector.
(2). no element is in the same position among the new vectors.
The following two are such vectors:
9 4 7 1 2 5 3 10 6 8
5 7 4 2 3 8 9 6 10 1
Here is one way to do this :
#Define the vector
V1 <- 1:10
#Number of rows
n <- 7
#Create a matrix with the vector `V1` in each row
mat <- matrix(V1, ncol = length(V1), nrow = n, byrow = TRUE)
i <- 2
while(i <= n) {
#Get randomised V1
temp <- sample(V1)
#Check if any of the previous row does not have the same combination
if (all(colSums(t(t(mat) == temp)) == 0)) {
#Add the random vector in the matrix
mat[i, ] <- temp
i <- i + 1
}
}
mat
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 1 2 3 4 5 6 7 8 9 10
#[2,] 4 5 6 7 10 2 1 9 3 8
#[3,] 2 6 7 9 1 3 8 10 5 4
#[4,] 9 3 1 2 4 8 6 5 10 7
#[5,] 7 8 5 3 6 9 10 4 1 2
#[6,] 3 1 4 5 8 10 9 7 2 6
#[7,] 8 4 2 10 9 1 5 6 7 3

find max value's column index and row index individually in R

I find people use
which(matrix==max(matrix, na.rm=FALSE))
to show both row and column index.
But my question is how do I extract row index and column index individually and then return these two values into another parameters?
like matrix=
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 6 7 7 2 4 3 7 1 4
[2,] 1 9 8 7 2 6 10 9 5 2
[3,] 7 10 8 4 10 5 4 8 4 4
[4,] 4 3 1 1 3 3 9 7 4 2
[5,] 1 8 1 9 9 8 1 3 7 7
[6,] 2 6 7 5 6 10 4 6 15 1
the max value is matrix[6,9]=15 how could I find row =6 and column = 9 separately and return 6 to a parameter:A, 9 to parameter:B
Thank you guys very much.
For a large matrix which.max should be more efficient than which. So, for a matrix m, we can use
A = row(m)[d <- which.max(m)]
B = col(m)[d]
Maybe a roundabout way but if the matrix is called "mat":
colmax <- {which(mat == max(mat)) %/% nrow(mat)} + 1
rowmax <- which(mat == max(mat)) %% nrow(mat)

Matrix with integer random rows

The command
matrix(sample.int(12, 9*12, TRUE), 9, 12)
generates an integer random matrix (9 rows and 12 columns) with integer values from 1 to 12. I wonder if there is a version of this code that generates a matrix whose rows are integer random rows with value from 1 to 12 (without repetition). I was able to find a "trivial" answer to this question; with
matrix(sample.int(m, 1*12), 9, 12, byrow=TRUE)
I obtain a matrix of this kind, but the rows are all equal to each other (this is the same row repeated 9 times).
The replicate function (which repeats an operation like sample(12) a specified number of times) returns a matrix whose column major orientation can be flipped to your desired row orientation with t:
t( replicate(9, {sample(12)} ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 9 11 5 3 4 6 2 8 10 12 7 1
[2,] 4 5 12 6 8 2 9 1 11 10 7 3
[3,] 9 8 10 12 2 6 3 7 4 1 11 5
[4,] 4 9 1 2 6 11 8 5 7 3 12 10
[5,] 1 2 4 5 11 6 3 8 10 9 12 7
[6,] 4 8 10 12 5 9 2 7 11 1 3 6
[7,] 5 7 8 4 1 6 10 11 2 3 12 9
[8,] 2 4 10 1 12 5 7 6 11 3 8 9
[9,] 2 7 9 11 8 1 12 10 6 5 3 4
The replicate function is used in a lot of simulation code.

How can I find repeated values/ data points and their index in 2D matrix of a dataframe in R?

For example suppose I have matrix A
x y z f
1 1 2 A 1005
2 2 4 B 1002
3 3 2 B 1001
4 4 8 C 1001
5 5 10 D 1004
6 6 12 D 1004
7 7 11 E 1005
8 8 14 E 1003
From this matrix I want to find the repeated values like 1001, 1005, D, 2 (in third column) and I also want to find their index (which row, or which position).
I am new to R!
Obviously it is possible to do with simple searching element by element by using a for loop, but I want to know, is there any function available in R for this kind of problem.
Furthermore, I tried using duplicated and unique, both functions are giving me the duplicated row number or column number, they are also giving me how many of them were repeated, but I can not search for whole matrix using both of them!
You can write a rather simple function to get this information. Though note that this solution works with a matrix. It does not work with a data.frame. A similar function could be written for a data.frame using the fact that the data.frame data structure is a subset of a list.
# example data
set.seed(234)
m <- matrix(sample(1:10, size=100, replace=T), 10)
find_matches <- function(mat, value) {
nr <- nrow(mat)
val_match <- which(mat == value)
out <- matrix(NA, nrow= length(val_match), ncol= 2)
out[,2] <- floor(val_match / nr) + 1
out[,1] <- val_match %% nr
return(out)
}
R> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 8 6 6 7 6 7 4 10 6 9
[2,] 8 6 6 3 10 4 5 4 6 9
[3,] 1 6 9 2 9 2 3 6 4 2
[4,] 8 6 7 8 3 9 9 4 9 2
[5,] 1 1 5 6 7 1 5 1 10 6
[6,] 7 5 4 7 8 2 4 4 7 10
[7,] 10 4 7 8 3 1 8 6 3 4
[8,] 8 8 2 2 7 5 6 4 10 4
[9,] 10 2 9 6 6 9 7 2 4 7
[10,] 3 9 9 4 2 7 7 2 9 6
R> find_matches(m, 8)
[,1] [,2]
[1,] 1 1
[2,] 2 1
[3,] 4 1
[4,] 8 1
[5,] 8 2
[6,] 4 4
[7,] 7 4
[8,] 6 5
[9,] 7 7
In this function, the row index is output in column 1 and the column index is output in column 2

An extension of selecting rows with same results in different columns

I found this while searching for a similar approach.
Selecting rows with same result in different columns in R
Is there a way to search within a range of columns? Playing off the example in the link, what if instead of catch[catch$tspp.name == catch$elasmo.name,], is it possible to do this?
catch[catch$tspp.name == c[23:56],] where R would search for values within columns 23 to 56 that match the tspp value?
Thanks in advance and please let me know whether it's better to post an independent question on a topic related to a previous post or to insert a follow up question within the aforementioned post.
Here's one way to do it. This finds rows of X where the first column appears in columns 2 through 9.
> set.seed(1)
> X<-matrix(sample(10,100,T),10)
> X
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 3 10 5 9 5 10 4 5 3
[2,] 4 2 3 6 7 9 3 9 8 1
[3,] 6 7 7 5 8 5 5 4 4 7
[4,] 10 4 2 2 6 3 4 4 4 9
[5,] 3 8 3 9 6 1 7 5 8 8
[6,] 9 5 4 7 8 1 3 9 3 8
[7,] 10 8 1 8 1 4 5 9 8 5
[8,] 7 10 4 2 5 6 8 4 2 5
[9,] 7 4 9 8 8 7 1 8 3 9
[10,] 1 8 4 5 7 5 9 10 2 7
> X[rowSums(X[,1]==X[,2:9])>0,]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 3 10 5 9 5 10 4 5 3
[2,] 3 8 3 9 6 1 7 5 8 8
[3,] 9 5 4 7 8 1 3 9 3 8
[4,] 7 4 9 8 8 7 1 8 3 9

Resources