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

看板R_Language作者 (nh)時間10年前 (2014/11/02 22:42), 編輯推噓2(205)
留言7則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《Edster (Edster)》之銘言: : best <- function(x, y){ : z <- c("heart attack", "heart failure", "pneumonia") : outcome <- read.csv(file="outcome-of-care-measures.csv", header=T, as.is=T) : SS <- as.numeric(outcome[,11]) : if (! (x %in% outcome$State) | ! (y %in% z)){ : stop("Error in best(state, outcome) : invalid state") : } : if (y == "heart attack"){ : select = outcome$State == x & !is.na(SS); o = order(SS) : result <- outcome[o[select],c(1,2)] : print(paste("best (", result, ") heart attack")) : } : } 請問從倒數第三行(select = outcome$State....)之後, 為何我用 o <- order(SS[select]) outcome.order <- outcome[o, ] 結果outcome.order還是有NA值存在? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.175.163.115 ※ 文章網址: http://www.ptt.cc/bbs/R_Language/M.1414939348.A.EBB.html

11/04 23:26, , 1F
看來問題出現在order. 那麼就得要老老實實的把
11/04 23:26, 1F

11/04 23:26, , 2F
order 跟 is.na 分開了.
11/04 23:26, 2F

11/04 23:51, , 3F
outcome = outcome[order(as.numeric(outcome[,11])),]
11/04 23:51, 3F

11/04 23:53, , 4F
select = outcome$State == x & !is.na(
11/04 23:53, 4F

11/04 23:53, , 5F
(as.numeric(outcome[,11]))
11/04 23:53, 5F

11/04 23:53, , 6F
result <- outcome[select,c(1,2)]
11/04 23:53, 6F

11/04 23:55, , 7F
變得有點醜... 現下也想不到更好的方式.
11/04 23:55, 7F
文章代碼(AID): #1KLaBKwx (R_Language)
文章代碼(AID): #1KLaBKwx (R_Language)