Мы могли бы использовать between
и case_when
library(dplyr)
credit_fun <- function(scores) {
case_when(between(scores, 300, 579) ~ "Very Poor",
between(scores, 580, 669) ~ "Fair",
between(scores, 670, 739) ~ "Good",
between(scores, 740, 779) ~ "Very Good",
TRUE ~ "Exceptional")
}
Или другой вариант cut
credit_fun <- function(scores) {
cut(scores, breaks = c(-Inf, 300, 580, 670, 740, Inf),
labels = c("Very Poor", "Fair", "Good", "Very Good", "Exceptional"))
}
Или с findInterval
credit_fun <- function(scores) {
c("Very Poor", "Fair", "Good", "Very Good", "Exceptional")[findInterval(scores,
c(300, 580, 670, 740))]
}