What is the method for multi-condition matching in the R language?
In the R language, you can use the ifelse() function to implement multiple condition matching. The usage of the ifelse() function is as follows:
ifelse(condition, value_if_true, value_if_false)
In this case, condition is a logic condition, value_if_true is the value returned when the condition is TRUE, and value_if_false is the value returned when the condition is FALSE.
Here is an example showing how to use the ifelse() function for multiple condition matching:
# 创建一个包含多个值的向量
x <- c(1, 3, 5, 7, 9)
# 使用ifelse函数对向量中的值进行判断
result <- ifelse(x < 5, "Small", ifelse(x > 5, "Large", "Medium"))
# 输出结果
print(result)
The output is as follows:
[1] "Small" "Small" "Medium" "Large" "Large"
In the example above, we use the ifelse() function to evaluate each value in the vector x. If the value is less than 5, it returns “Small”, if it is greater than 5, it returns “Large”, and if it is equal to 5, it returns “Medium”.