【发布时间】:2016-01-02 17:07:41
【问题描述】:
我需要以下 R 代码的帮助 - 我的代码是:
best <- function(state, outcome) {
## Read outcome data
outcome <- read.csv("outcome-of-care-measures.csv", colClasses = "character") enter code here
## Check that state and outcome are valid using STOP function and
## message "invalid state" or "invalid outcome"
valid_outcomes <- c("heart attack", "heart failure", "pneumonia")
if(!state %in% state.abb){
stop("Invalid state")
}
else if (!outcome %in% valid_outcomes)
{
stop("Invalid outcome")
}
return(x)
## Return hospital name in that state with lowest 30-day death ## rate
}
返回:
Error in best("NY", "health") : Invalid outcome
In addition: Warning message:
In if (!outcome %in% valid_outcomes) { :
the condition has length > 1 and only the first element will be used
如果我使用 else if (any(!outcome %in% valid_outcomes)) 似乎有帮助,但逻辑条件没有得到正确解决。
关于如何解决这个问题的任何想法?谢谢
【问题讨论】:
标签: r string conditional-statements