find second maximum position in matrix (like max.col) - r

I'm trying to figure out a way to find the second maximum position for each row of a matrix, just like max.col function does, but for the second highest value.
Any ideas?
Thanks!

You could try to replace the maximum value with -Inf and then take max.col a second time:
set.seed(1)
mat <- matrix(sample(1:15), nrow = 5)
#mat
# [,1] [,2] [,3]
#[1,] 4 9 2
#[2,] 6 10 13
#[3,] 8 14 12
#[4,] 11 5 15
#[5,] 3 1 7
max.col(replace(mat, cbind(1:5, max.col(mat)), -Inf))
#[1] 1 2 3 1 1
Using a second max.col is handy because if you had ties, you can still use max.col to handle them appropriately.

Related

how to select neighbouring elements in a vector and put them into a list or matrix in R

I have a problem about how to select neighboring elements in a vector and put them into a list or matrix in R.
For example:
vl <- c(1,2,3,4,5)
I want to get the results like this:
1,2
2,3
3,4
4,5
The results can be in a list or matrix
I know we can use a loop to get results.Like this:
pl <- list()
k=0
for (p in 1: length(vl)) {
k=k+1
pl[[k]] <- sort(c(vl[p],vl[p+1]))}
But I have a big data. Using loop is relatively slow.
Is there any function to get results directly?
Many thanks!
We can use head and tail to ignore the last and first element respectively.
data.frame(a = head(vl, -1), b = tail(vl, -1))
# a b
#1 1 2
#2 2 3
#3 3 4
#4 4 5
EDIT
If the data needs to be sorted we can use apply row-wise to sort it.
vl <- c(2,5,3,1,6,4)
t(apply(data.frame(a = head(vl, -1), b = tail(vl, -1)), 1, sort))
# [,1] [,2]
#[1,] 2 5
#[2,] 3 5
#[3,] 1 3
#[4,] 1 6
#[5,] 4 6
You can do:
matrix(c(vl[-length(vl)], vl[-1]), ncol = 2)
[,1] [,2]
[1,] 1 2
[2,] 2 3
[3,] 3 4
[4,] 4 5
If you want to sort two columns rowwise, then you can use pmin() and pmax() which will be faster than using apply(x, 1, sort) with a large number of rows.
sapply(c(pmin, pmax), do.call, data.frame(vl[-length(vl)], vl[-1]))
The problem can also be solved by applying the sort() function on a rolling window of length 2:
vl <- c(2,5,3,1,6,4)
zoo::rollapply(vl, 2L, sort)
which returns a matrix as requested:
[,1] [,2]
[1,] 2 5
[2,] 3 5
[3,] 1 3
[4,] 1 6
[5,] 4 6
Note that the modified input vector vl is used which has been posted by the OP in comments here and here.
Besides zoo, there are also other packages which offer rollapply functions, e.g.,
t(rowr::rollApply(vl, sort, 2L, 2L))

Column having maximum L2 norm

How to find the column of a matrix which has the maximum L2 norm? The matrix has NA values in some columns, we want to ignore those columns.
The following code I am trying, but it shows error due to NA values.
#The matrix is T
for(i in 1:ncol(T)){
if(norm(y,type='2') < norm(T[,i],type = '2'))
y = T[,i]
}
I think it would also be useful if we could somehow get the columns of T as a list, since we could use which.max function then, but I could not do that. Is that possible?
Please help
Maybe you can write your own L2 norm and find the column with the maximum, i.e.,
which.max(sqrt(colSums(T**2)))
Example
T <- matrix(c(1:10,NA,12:19,NA),nrow = 4)
> T
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 NA 15 19
[4,] 4 8 12 16 NA
> which.max(sqrt(colSums(T**2)))
[1] 4

find the column with lowest value in r

I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3)). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row.
How should I do it? It could be duplicated question but I could not find answers.
[,1] [,2] [,3] [,4]
[1,] 10 11 1 12
[2,] 8 9 7 3
[3,] 2 5 6 4
Use max.col:
max.col(-mat)
# [1] 3 4 1

R: How to convert a vector into matrix without replicating the vector?

Here's my problem:
I have a vector and I want to convert it into a matrix with fixed number of columns, but I don't want to replicate the vector to fill the matrix when it's necessary.
For example:
My vector has a length of 15, and I want a matrix with 4 columns.I wish to get the matrix wit 15 elements from the vector and a 0 for the last element in the matrix.
How can I do this?
Edit:
Sorry for not stating the question clearly and misguiding you guys with my example. In my program,I don't know the length of my vector, it depends on other parameters and this question involves with a loop, so I need a general solution that can solve many different cases, not just my example.
Thanks for answering.
You could subset your vector to a multiple of the number of columns (so as to include all the elements). This will add necessary amount of NA to the vector. Then convert to matrix.
x = 1:15
matrix(x[1:(4 * ceiling(length(x)/4))], ncol = 4)
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
If you want to replace NA with 0, you can do so using is.na() in another step
We can also do this with dim<- and length<-
n <- 4
n1 <- ceiling(length(x)/n)
`dim<-`(`length<-`(x, n*n1), c(n1, n))
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 NA
data
x <- 1:15

matrix row merge in R

I have a matrix
[,1] [,2]
[1,] 2 3
[2,] 3 5
[3,] 7 9
[4,] 11 3
[5,] 11 8
and I want to merge row 1 2 4 5 by their common value.
the result should be output
2 3 5 11 8
Test case:
m <- matrix(c(2,3,7,11,11,3,5,9,3,8),ncol=2)
I'm not sure this is what you want, but it gives the right answer:
unique(c(t(m[c(1,2,4,5),])))
Only two tricky bits here:
need to use c() to collapse the matrix into a single vector
need to use t() to get the matrix collapsed row-wise rather than column-wise to get the ordering as you specified.

Resources