Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R If条件语句错误:“}”中出现意外的“}”_R_If Statement - Fatal编程技术网

R If条件语句错误:“}”中出现意外的“}”

R If条件语句错误:“}”中出现意外的“}”,r,if-statement,R,If Statement,当我运行代码时,它抛出错误:}where中出现意外的“}” else num==最糟糕的部分,所以如果我从else中更改这个if,它将下降并在else is.numericnum中再次抛出一个错误。我用这个代码做错了什么?我想如果声明是这样的话 如果 否则如果 否则如果 其他的 另外,你可以像我使用的那样在它们里面添加加法If/else语句。也许不是因为它会给我带来一个错误,有人能看到它有什么问题并告诉我如何解决吗?否则If和else必须与If表达式的结尾在同一行,它们之间不能换行。您可以使用大

当我运行代码时,它抛出错误:}where中出现意外的“}” else num==最糟糕的部分,所以如果我从else中更改这个if,它将下降并在else is.numericnum中再次抛出一个错误。我用这个代码做错了什么?我想如果声明是这样的话

如果 否则如果 否则如果 其他的 另外,你可以像我使用的那样在它们里面添加加法If/else语句。也许不是因为它会给我带来一个错误,有人能看到它有什么问题并告诉我如何解决吗?

否则If和else必须与If表达式的结尾在同一行,它们之间不能换行。您可以使用大括号简洁地表达这一点,例如:

rankhospital <- function(state, outcome, num = "best"){
      ## Read outcome data
      data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
      hospital <- data.frame(data[,2], #name 
                             data[,7], #state
                             data[,11], #heart attack
                             data[,17], # heart failure
                             data[,23], stringsAsFactors = FALSE) #pneumonia
      colnames(hospital) <- c("name", "state", "heart attack", "heart failure", "pneumonia")
      ## Check that state and outcome are valid
      if (!state %in% hospital[, "state"]) { stop('invalid state')} 
      else if (!outcome %in% c("heart attack", "heart failure", "pneumonia")){ stop('invalid outcome')}
      else if (is.character(num)){
        if (num == "best") {
          chosen_state <- hospital[which(hospital[, "state"] == state), ] #select the state
          chosen_outcome <- chosen_state[ , outcome] #select the outcome from the state
          best_outcome <- chosen_state[which(chosen_outcome == min(chosen_outcome, na.rm = TRUE)),]["name"]
          best_hospital <- best_outcome[order(best_outcome)][ , "name"] #sort
          best_hospital
        } 
        else (num == "worst") {
          chosen_state <- hospital[which(hospital[, "state"] == state), ] #select the state
          chosen_outcome <- chosen_state[ , outcome] #select the outcome from the state
          best_outcome <- chosen_state[which(chosen_outcome == max(chosen_outcome, na.rm = TRUE)),]["name"]
          best_hospital <- best_outcome[order(best_outcome)][ , "name"] #sort
          best_hospital
        } 
      else (is.numeric(num)) {
        if (num =< length(hospital$name){
          chosen_state <- hospital[which(hospital[, "state"] == state), ] #select the state
          chosen_outcome <- as.numeric(chosen_state[ , outcome]) #select the outcome from the state
          chosen_state[which(sort(chosen_outcome)) == num, ][,"name"]

       } 
        else (num > length(hospital$name) { stop('NA')}

     } 

    }

您使用的if/else语法不正确;else捕获if不满意的所有案例。所以对于else没有更明确的条件。例如:a你是什么意思?你能指出使用我上面的代码吗?我是编程新手,if/else语法真的让我困惑..它在包含else的每一行中都是不正确的;例如,行else is.numericnum{should be}else{等等,请注意括号…谢谢,我现在明白了!是的,这是OP代码的一个问题;另一个问题是else之后的错误条件语句。当您延迟键入closing}时,在else之前可能有多行。
x = 3

# This doesn't work
if (x == 2) { print("x is 2") }
else if (x == 3) { print("x is 3") }
else { print("x is something else")}

# This does, else if is on the same line as the end of the
#   if expression
if (x == 2) { 
    print("x is 2") 
} else if (x == 3) { 
    print("x is 3") 
} else { print("x is something else")}