I have a matrix of correlations, and I am trying to keep the maximum value (considering absolute values) of each pair (row/column). I wanted to ask, how to extract the values if I have the index of the position with the specific max. value. This is my sample:
mat <- structure(c(0, 0.428291512801413, 0.124436112431533, -0.345870125921382,
0.391613957773281, 0.428291512801413, 0, 0.341415068127906, -0.346724601510298,
0.486360835614514, 0.124436112431533, 0.341415068127906, 0, -0.496213980990412,
0.41819049956841, -0.345870125921382, -0.346724601510298, -0.496213980990412,
0, -0.80231408836218, 0.391613957773281, 0.486360835614514, 0.41819049956841,
-0.80231408836218, 0), .Dim = c(5L, 5L), .Dimnames = list(c("LO3","Tx", "Gh", "RH", "SR"), c("LO3", "Tx", "Gh", "RH", "SR")))
Then, I am taking the index of maximum value:
ind <- apply(abs(mat), 2, which.max)
which gives me:
LO3 Tx Gh RH SR
2 5 4 5 4
What I wanted now, it's getting the value of these position for each column .. which would be:
LO3 Tx Gh
0.4282915 0.4863608 -0.4962140 .....
I tried to use apply
, but I don't know how to do it..or if there would be another way to do that.
Since you have your indices in ind
one way could be to use mapply
:
#the first argument is the function call
#second argument is your matrix coerced to data.frame
#third argument is your indices
#each time an index will be used in conjunction to a column
#and you get your result
mapply(function(x,y) x[y], as.data.frame(mat), ind)
# LO3 Tx Gh RH SR
# 0.4282915 0.4863608 -0.4962140 -0.8023141 -0.8023141
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments