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

看板R_Language作者 (nh)時間10年前 (2014/10/28 23:02), 編輯推噓3(303)
留言6則, 2人參與, 最新討論串1/3 (看更多)
[問題類型]: 程式諮詢(我想用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
變數加了引號就變字串, 你的 "x" 跟 "y" 應該要改成x, y
10/28 23:49, 2F

10/29 00:03, , 3F
best <- function(x, y){
10/29 00:03, 3F

10/29 00:03, , 4F
z <- c("heart attack", "heart failure", "pneumonia")
10/29 00:03, 4F

10/29 00:04, , 5F
我還是回文好了.
10/29 00:04, 5F

10/29 18:44, , 6F
還以為問題是 else 寫錯 ㄎㄎ
10/29 18:44, 6F
文章代碼(AID): #1KJw_rjN (R_Language)
文章代碼(AID): #1KJw_rjN (R_Language)