Given the following matrix:
A B C
[1,] TRUE FALSE TRUE
[2,] FALSE TRUE TRUE
[3,] FALSE FALSE TRUE
[4,] FALSE TRUE TRUE
[5,] FALSE TRUE TRUE
[6,] TRUE TRUE TRUE
m <- structure(c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), .Dim = c(6L,
3L), .Dimnames = list(NULL, c("A", "B", "C")))
How we can extract the first column with TRUE value per row efficiently? Of course, we could use apply
per row and then get the min(which(...))
.
Here is the desired output:
[1] A B C B B A
This thread might seem a duplicate of my question but its not:
- Here we are talking about a logical matrix NOT a numeric data frame
- Here we seek to get the position of the first TRUE not the highest value