此解决方案可能对您有用:
library(tidyverse)
people <- c("Anne", "Betty", "Carol", "Darrel")
low <- c(1, 1, 0, 0)
medium <- c(1, 0, 0, 0)
high <- c(0, 1, 1, 0)
df <- tibble(people, low, medium, high)
df %>% dplyr::mutate(lowest_score = ifelse(low == 1, "low",
ifelse(medium == 1, "medium",
ifelse(high == 1, "high", "no_response"))))
# A tibble: 4 x 5
people low medium high lowest_score
<chr> <dbl> <dbl> <dbl> <chr>
1 Anne 1 1 0 low
2 Betty 1 0 1 low
3 Carol 0 0 1 high
4 Darrel 0 0 0 no_response
如果该列的值等于 1,则通过从低 -> 中 -> 高逐步测试来改变一个新列“lowest_score”。如果该列的值为 1,则它停止查找并记录第一个 1 出现的列,如果该列是 0 它移动到下一列。如果全为 0,则报告“no_response”。不确定你是否预料到这一点。