我在兩列中有一個 TRUE/FALSE 陳述句串列。我想確定測驗是否在失敗之前通過,測驗是否在通過之前失敗,或者兩者都不通過。我也在嘗試為每種情況創建一個輸出。
因此,例如,如果測驗在失敗之前通過
Pass Fail
1 TRUE FALSE
2 FALSE FALSE
3 FALSE FALSE
我希望輸出說明發生了哪個事件,然后說明它發生的行號,因此對于上面的示例,輸出看起來像這樣
Pass 1
另一個失敗的測驗示例
Pass Fail
1 FALSE FALSE
2 FALSE TRUE
3 TRUE FALSE
預期的輸出看起來像
Fail 2
然后是沒有通過或失敗的情況
Pass Fail
1 FALSE FALSE
2 FALSE FALSE
3 FALSE FALSE
并且預期的輸出將是
None 0
正如我之前提到的,我想找出首先發生的事件。
通過資料集
structure(list(Pass = c(TRUE, FALSE, FALSE), Fail = c(FALSE,
FALSE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
失敗資料集
structure(list(Pass = c(FALSE, FALSE, TRUE), Fail = c(FALSE,
TRUE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
無資料集
structure(list(Pass = c(FALSE, FALSE, FALSE), Fail = c(FALSE,
FALSE, FALSE)), row.names = c(NA, 3L), class = "data.frame")
uj5u.com熱心網友回復:
當只有一個值感興趣時,與場景相關的一種選擇可能是:
fun <- function(data) {
ind <- sapply(data, function(x) match(TRUE, x))
if(all(is.na(ind))) {
setNames(0, "None")
} else {
ind[which.min(ind)]
}
}
第一個資料集的結果:
Pass
1
第二:
Fail
2
第三:
None
0
如果輸出應該是一個資料幀,那么它可以調整為:
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
uj5u.com熱心網友回復:
一個簡單的base解決方案:
解決方案
find_result <- function(data) {
# Make a vector naming the first occurrence of each result.
firsts <- c("Pass" = which(data$Pass)[1], "Fail" = which(data$Fail)[1])
# If there are no occurrences, default to "None".
if(all(is.na(firsts)))
data.frame(Result = "None", Row = 0)
# Otherwise locate and name the earliest of the two occurrences.
else {
which <- which.min(firsts)
data.frame(Result = names(firsts)[which], Row = unname(firsts)[which])
}
}
結果
鑒于您在此處復制的示例資料
pass_df <- structure(
list(
Pass = c(TRUE, FALSE, FALSE),
Fail = c(FALSE, FALSE, FALSE)
),
row.names = c(NA, 3L),
class = "data.frame"
)
fail_df <- structure(
list(
Pass = c(FALSE, FALSE, TRUE),
Fail = c(FALSE, TRUE, FALSE)
),
row.names = c(NA, 3L),
class = "data.frame"
)
none_df <- structure(
list(
Pass = c(FALSE, FALSE, FALSE),
Fail = c(FALSE, FALSE, FALSE)
),
row.names = c(NA, 3L),
class = "data.frame"
)
該find_result()函式應產生以下結果:
#> find_result(pass_df)
Result Row
1 Pass 1
#> find_result(fail_df)
Result Row
1 Fail 2
#> find_result(none_df)
Result Row
1 None 0
uj5u.com熱心網友回復:
這是一種使用方法dplyr::cumany:
PF <- function(data){
mymin <- function(x) ifelse(!all(is.na(x)), min(x, na.rm=T), NA)
npass <- sum(cumany(data$Pass))
firstPass <- mymin(which(data$Pass == T))
nfail <- sum(cumany(data$Fail))
firstFail <- mymin(which(data$Fail == T))
if(npass > nfail) c(result = "pass", which = firstPass) else
if (npass < nfail) c(result = "fail", which = firstFail) else
c(result = "none", which = 0)
}
PF(pass)
result which
"pass" "1"
PF(fail)
result which
"fail" "2"
PF(none)
result which
"none" "0"
uj5u.com熱心網友回復:
我會通過首先找到輸入中任何列所在的第一行來解決此問題TRUE。然后找到該TRUE行上的列。更聰明的版本使用加權和來準確確定連續發生的事件:
find_event <- function(x) {
# Encode event combination on each row as a binary integer
codes <- rowSums(2^(col(x) - 1) * x)
# Find first row with any events
row <- head(which(codes > 0), 1)
# Decode the event combination on that row
cols <- which(intToBits(codes[row]) > 0)
events <- colnames(x)[cols]
if (length(events) == 0) {
data.frame(event = "None", row = 0L)
} else {
data.frame(event = events, row = row)
}
}
對原始示例資料的測驗:
pass <- data.frame(
Pass = c(TRUE, FALSE, FALSE),
Fail = c(FALSE, FALSE, FALSE)
)
find_event(pass)
#> event row
#> 1 Pass 1
fail <- data.frame(
Pass = c(FALSE, FALSE, TRUE),
Fail = c(FALSE, TRUE, FALSE)
)
find_event(fail)
#> event row
#> 1 Fail 2
none <- data.frame(
Pass = c(FALSE, FALSE, FALSE),
Fail = c(FALSE, FALSE, FALSE)
)
find_event(none)
#> event row
#> 1 None 0
還有一些更奇特的資料:
pass_fail <- data.frame(
Pass = c(FALSE, TRUE, TRUE),
Fail = c(FALSE, TRUE, FALSE)
)
find_event(pass_fail)
#> event row
#> 1 Pass 2
#> 2 Fail 2
indeterminate <- data.frame(
Pass = c(FALSE, FALSE, TRUE),
Fail = c(FALSE, FALSE, TRUE),
Indeterminate = c(TRUE, FALSE, FALSE)
)
find_event(indeterminate)
#> event row
#> 1 Indeterminate 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418394.html
標籤:
下一篇:修復ggplot中的圖例
