R - testing matrix values based on vector -
i have 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 set of vectors corresponding maximum values each column.
max <- apply(blah,2,max) > max [1] 10 10 8 9 9 i want return logical vector based on 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 can implement loop on columns this, there elegant r-type way accomplish this?
apply(blah,2,function(x) {x == max(x)}) this hit me. it's simple. can't believe didn't think of earlier.
Comments
Post a Comment