简体   繁体   English

利用逻辑向量中的if else循环来区分R中数值的三种状态

[英]Utilize if else loop in logical vector to distinguish three statuses of numerical values in R

I want to convert numerical value to logical vector and compare it 5, this is the output that I want ( I wanted to compare logical vector to logical object, then if else loop to low, optimal or high):我想将数值转换为逻辑向量并将其比较 5,这是我想要的输出(我想将逻辑向量与逻辑对象进行比较,然后 if else 循环到低、最优或高):

5,2,8,6,11,4,7

 5 is optimal.
 Caution: 2 is low.
 8 is high.
 6 is high.
 11 is high.
 Caution: 4 is low .
 7 is high.

I tried the following:我尝试了以下方法:

a<-c(5,2,8,6,11,4,7) #assign values to a vector
a3<-unlist(a, recursive = TRUE, use.names = TRUE)
an<-as.numeric(a3) #convert to numeric
st5<-5   #set a reference 
st1<-as.logical(st5)  #convert to logical
suffice <-(n3 >= 5) #assign output of 7 True and False into a variable
cat("\n")
cat("5 is norm \n")
cat("Data: ", paste(shQuote(a), "pg", collapse=", "), "\n")
if(a > 5) {
    paste(co1, "is high \n") if (a = 5) {
        paste(a, "is optimal.\n")
        }
        else {paste("Caution:", a, "is low \n"))}
        }
    })

How about this?这个怎么样?

a<-c(5,2,8,6,11,4,7) #assign values to a vector
n=5 #assign your optimal value
ifelse(a==n, paste0(n, " is optimal."), ifelse(a>n, paste0(a, " is high."), paste0("Caution: ",a," is low.")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM