Reputation: 491
I have a matrix
blah <- structure(c(10L, 7L, 5L, 3L, 8L, 10L, 3L, 9L, 3L, 3L, 4L, 8L, 7L, 4L, 8L, 5L, 2L, 1L, 9L, 7L, 6L, 7L, 9L, 3L, 3L), .Dim = c(5L, 5L))
> blah
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 4 5 6
[2,] 7 3 8 2 7
[3,] 5 9 7 1 9
[4,] 3 3 4 9 3
[5,] 8 3 8 7 3
I have a set of vectors corresponding to the maximum values for each column.
max <- apply(blah,2,max)
> max
[1] 10 10 8 9 9
I want to return a logical vector based on the largest value per column
[,1] [,2] [,3] [,4] [,5]
[1,] T T F F F
[2,] F F T F F
[3,] F F F F T
[4,] F F F T F
[5,] F F T F F
I know I can implement a loop over the columns to do this, but is there an elegant R-type way to accomplish this?
Upvotes: 1
Views: 69
Reputation: 491
apply(blah,2,function(x) {x == max(x)})
This just hit me. It's so simple. Can't believe I didn't think of this earlier.
Upvotes: 4