I have a list of TRUE/FALSE statements in two columns. I want to determine if a test passes before it fails, a test fails before it passes, or neither. I am trying to create an output for each situation as well.
So for example if a test passes before it fails
Pass Fail
1 TRUE FALSE
2 FALSE FALSE
3 FALSE FALSE
I want the output to say which event occurred and then the row number at which it occurred so for the example above the output would look like this
Pass 1
Another example of a test that failed
Pass Fail
1 FALSE FALSE
2 FALSE TRUE
3 TRUE FALSE
The expected output would look like
Fail 2
And then a situation where there was no pass or fail
Pass Fail
1 FALSE FALSE
2 FALSE FALSE
3 FALSE FALSE
and the expected output would be
None 0
As I mentioned before, I want to find out which event occurs first.
Pass dataset
structure(list(Pass = c(TRUE, FALSE, FALSE), Fail = c(FALSE,
FALSE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
Fail dataset
structure(list(Pass = c(FALSE, FALSE, TRUE), Fail = c(FALSE,
TRUE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
None dataset
structure(list(Pass = c(FALSE, FALSE, FALSE), Fail = c(FALSE,
FALSE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
One option relevant for scenarios when only one value is of interest could be:
fun <- function(data) {
ind <- sapply(data, function(x) match(TRUE, x))
if(all(is.na(ind))) {
setNames(0, "None")
} else {
ind[which.min(ind)]
}
}
Results for the first dataset:
Pass
1
second:
Fail
2
third:
None
0
If the output is supposed to be a dataframe, then it could be adjusted to:
fun <- function(data) {
ind <- sapply(data, function(x) match(TRUE, x))
if(all(is.na(ind))) {
stack(setNames(0, "None"))
} else {
stack(ind[which.min(ind)])
}
}
values ind
1 2 Fail
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With