[問題] if elso loop不能接著執行?

作者: nh2 (nh)   2014-10-28 23:02:09
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
請把以下不需要的部份刪除
新手(沒寫過程式,R 是我的第一次)
[問題敘述]:
其實這是coursera course的作業三,想要讀入資料後,先看州名和outcome
名符不符合,不符合顯示error,符合則顯示outcome最好的醫院
[程式範例]:
best <- function(x, y){
z <- c("heart attack", "heart failure", "pneumonia")
##read oucome data
outcome <- read.csv(file="outcome-of-care-measures.csv", header=T)
outcome[,11] <- as.numeric(as.character(outcome[,11]))
## check if state and outcome are valid
if (! ("x" %in% outcome$State)){
print("Error in best(state, outcome) : invalid state")
} else if (! ("y" %in% z)){
print("Error in best(state, outcome) : invalid state")
} else if ("y" == "heart attack"){
##return name with lowest 30 day death
outcome_select <- subset(outcome, outcome$State == "x")
outcome_sel_clean <-outcome_select[!is.na(outcome_select[,11]),] ##
remove NA
M_order <- order(outcome_sel_clean[,11]) ##establish order
outcome_sel_order <- outcome_sel_clean[M_order,] ## order as new order
result <- paste(outcome_sel_order[[1,2]], "heart attack", sep =",")
print(paste("best (", result, ")"))
}
接下來的程式碼差不多,但是不管x輸入哪個值,就算州名正確,也是跑出error…
因為從沒學過程式語言,只是自已想自修,沒想到這麼難…
另一個是小弟的程式碼確實可以再精簡,不過就等更熟了…
作者: Edster (Edster)   2014-10-28 23:48:00
這是什麼東西為什麼一直看到?變數加了引號就變字串, 你的 "x" 跟 "y" 應該要改成x, ybest <- function(x, y){z <- c("heart attack", "heart failure", "pneumonia")我還是回文好了.
作者: gsuper (Logit(odds))   2014-10-29 18:44:00
還以為問題是 else 寫錯 ㄎㄎ

Links booklink

Contact Us: admin [ a t ] ucptt.com