I have a matrix similar to below.
mat <- matrix(c(3,2,7,8,5,12,4,5,8,9,1,12,4,17,1,2,6,17,8,5),byrow=T,nrow=4)
colnames(mat) <- c(1,2,3,4,5); rownames(mat) <- c(1,2,3,4)
mat
1 2 3 4 5
1 3 2 7 8 5
2 12 4 5 8 9
3 1 12 4 17 1
4 2 6 17 8 5
I am wanting to get the maximum of each column which I can get using:
apply(mat,2,max)
1 2 3 4 5
12 12 17 17 9
But I would like to have the duplicate indexes in a list like:
[[1]]
[1] 1 2
[[2]]
[1] 3 4
[[3]]
[1] 5
How would I go about doing so?
Try
> unname(split(1:ncol(mat), Reduce(pmax, asplit(mat, 1))))
[[1]]
[1] 5
[[2]]
[1] 1 2
[[3]]
[1] 3 4
or
> aggregate(ind ~ ., stack(Reduce(pmax, asplit(mat, 1))), as.numeric)[["ind"]]
[[1]]
[1] 5
[[2]]
[1] 1 2
[[3]]
[1] 3 4