[问题] 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