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 can we extract the first column with a true value for a row efficiently ? Of course, we could use applyper line and then get min(which(...)).
Here is the desired result:
[1] A B C B B A
This thread may seem like a duplicate of my question, but it doesn't exist:
- Here we are talking about a logical matrix NOT a numerical data frame
- Here we strive to obtain the position of the first TRUE, and not the highest value
source
share