Question

before posting this question I have searched for something similar across several questions, but I wasn't able to find what I am looking for. If this post is a duplicated, well I apologize and I will appreciated if you can forward me to the right question.

I have the following data:

data<-data.frame(a=c(0:10),b=c(5:15),c=c(10:20),d=c(1:5,15:20))

and I am "simply" looking to get the column names of the row with the maximum values. I accomplish this task with

names(data)[apply(data,1,which.max)]

Actually, as there are two columns (c & d[6:10]) which fit the condition of maximum, what I really would like to obtain is something like:

result<-c("c","c","c","c","c","double","double","double","double","double")

So, as the behaviour of which.max only allows (at least it seems to me) to get only the first element, I figured what can be a really complicated solution. Finding the duplicated elements of each row and then, if any of these indexes matches with those from the code with which.max, change it with "double". More or less something like this:

index<-t(apply(data,1,function (x) duplicated(x,fromLast=TRUE)))
colnames(index)<-colnames(data)

Once again any tips would be more than appreciated!

Was it helpful?

Solution

May be you can simplify your approach by using directly which and arr.ind parameter.

data <- data.frame(a = c(0:10), b = c(5:15), c = c(10:20), d = c(1:5, 15:20))
ind <- which(data == max(data), arr.ind = TRUE)
ind
##      row col
## [1,]  11   3
## [2,]  11   4

names(data)[ind[,2]]
## [1] "c" "d"

EDIT

To have the same results per row

lapply(apply(data, 1, function(x) which(x == max(x), arr.ind = TRUE)), names)
## [[1]]
## [1] "c"

## [[2]]
## [1] "c"

## [[3]]
## [1] "c"

## [[4]]
## [1] "c"

## [[5]]
## [1] "c"

## [[6]]
## [1] "c" "d"

## [[7]]
## [1] "c" "d"

## [[8]]
## [1] "c" "d"

## [[9]]
## [1] "c" "d"

## [[10]]
## [1] "c" "d"

## [[11]]
## [1] "c" "d"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top