ramesh
ramesh

Reputation: 1217

find the index of max value in data frame and add the value


This is my data frame:

>head(dat)
  geno    P1    P2    P3    P4   dif
1   G1 0.015 0.007 0.026 0.951 0.001
2   G2 0.008 0.006 0.015 0.970 0.001
3   G3 0.009 0.006 0.017 0.968 0.000
4   G4 0.011 0.007 0.017 0.965 0.000
5   G5 0.013 0.005 0.021 0.961 0.000
6   G6 0.009 0.006 0.007 0.977 0.001

Here, I need to find max in each row and add dat$dif to the max.
when i used which.max(dat[,-1]), I am getting error:

Error in which.max(dat[,-1]) : 
  (list) object cannot be coerced to type 'double'

Upvotes: 0

Views: 2137

Answers (2)

hvollmeier
hvollmeier

Reputation: 2986

There's an app, eh function for that :-).

max.col finds the index of the maximum position for each row of a matrix. Take note, that as max.col expects a matrix (numeric values only) you have to exclude the “geno” column when applying this function.

sapply(1:6,function(x) dat[x,max.col(dat[,2:5])[x] +1]) + dat$dif
[1] 0.952 0.971 0.968 0.965 0.961 0.978

Upvotes: 0

r2evans
r2evans

Reputation: 161110

A previous answer (by Scriven) gives most of it but as others have stated, it incorrectly includes the last column. Here is one method that works around it:

idx <- (! names(dat) %in% c('geno','dif'))
dat$dif + apply(dat[,idx], 1, max)
#     1     2     3     4     5     6 
# 0.952 0.971 0.968 0.965 0.961 0.978 

You can easily put the idx stuff directly into the dat[,...] subsetting, but I broke it out here for clarity.

idx can be defined by numerous things here, such as "all but the first and last columns": idx <- names(dat)[-c(1, ncol(dat))]; or "anything that looks like P#": idx <- grep('^P[0-9]+', names(dat)).

Upvotes: 1

Related Questions