Select column and row values with an index in data frame

user3231352

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.

LyzandeR

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.

edited at
0

Comments

0 comments
Login to comment

Related

R add index column to data frame based on row values

Get row index in repetitive values in Data Frame

subtraction and division of row values in a data frame column

Using column values of a data frame to index rows of a multiindex data frame

Return column index of first set of consecutive values in data frame row in R

How to select row value from given columns based on comparison of other column values in Pandas data frame?

Filter pandas multi index data frame based on index column values

Match column and row names to column and values in another data frame

replacing the values of a matrix cells in a data frame based on the index and column values

Dividing values in a column of a data frame by values from a different data frame when row values match

how to select row index and variable name based on value in a data frame?

convert first column in data.frame to row index in R

Convert first column in data.frame to row index

Take a variable from a data frame row based on index found on a column

How to extract column and row index from a Data Frame that satisfies a condition

Find the column index of an element for each row of a data frame

Select data frame value based on specific row and column in R

Select rows of the multi-level index data frame with index values equal to columns in another data frame in pandas

Populate columns by matching name of column in data-frame with row values

Match row names and column names to values in another data frame

Selecting Top N Column Values per Row from a data frame

Change row values in specific pandas data frame column with python

How to rearrange data frame so that values in one column are row names?

Speedy test on R data frame to see if row values in one column are inside another column in the data frame

How do I Identify by row id the values in a data frame column not in another data frame column?

pandas - How to append a column to data frame by matching index values?

Find the index of certain values in a data frame and put it as a separate column

Getting column values from multi index data frame pandas

Reshape data frame, so the index column values become the columns