[問題] if elso loop不能接著執行?
[問題類型]:
程式諮詢(我想用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…
因為從沒學過程式語言,只是自已想自修,沒想到這麼難…
另一個是小弟的程式碼確實可以再精簡,不過就等更熟了…
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.175.167.72
※ 文章網址: http://www.ptt.cc/bbs/R_Language/M.1414508533.A.B57.html
推
10/28 23:48, , 1F
10/28 23:48, 1F
→
10/28 23:49, , 2F
10/28 23:49, 2F
推
10/29 00:03, , 3F
10/29 00:03, 3F
→
10/29 00:03, , 4F
10/29 00:03, 4F
→
10/29 00:04, , 5F
10/29 00:04, 5F
推
10/29 18:44, , 6F
10/29 18:44, 6F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 3 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章