AS.NUMERIC的某些功能无法按预期运行。我收到强制引入的新闻。也许我应该使用另一个功能。或者以不同的方式使用as.numeric。
当函数只导致一个结果时,它才会出错。排名时工作正常。如果没有as.numeric,则排名不能正常运行,因为它基于1进行排名,如前例1,1002,2,233,25 .....应该是1,2,25,233,1002。这是一个圆圈。
让我知道如何改进。
最佳(“MD”,“心脏病发作”) [1] NA 警告信息: 最好的(“MD”,“心脏病发作”):通过强制引入的NA 最好的( “MD”, “肺炎”) “更大的巴尔的摩医疗中心”
请参阅下面的函数和输出:
best<- function(state, outcome){
stateCorrect <- c("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY","GU")
outcomeCorrect <-c("heart attack","heart failure","pneumonia")
if(!(state %in% stateCorrect)){
stop('invalid state')
}
else if(!(outcome %in% outcomeCorrect)){
stop('invalid outcome')
}
thedata<-read.csv("outcome-of-care-measures.csv", colClasses = "character")
mylist <- split(thedata, thedata$State)
tx<-mylist[[state]]
if(outcome=="heart attack"){
k<-tx[["Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"]]
l<-k[!is.na(k)]
xtx<- min(as.numeric(l))
ytx<-which(tx[["Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"]] == xtx)
z<-tx[["Hospital.Name"]][ytx]
}
if(outcome=="heart failure"){
k<-tx[["Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"]]
l<-k[!is.na(k)]
xtx<- min(as.numeric(l))
ytx<-which(tx[["Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"]] == xtx)
z<-tx[["Hospital.Name"]][ytx]
}
if(outcome=="pneumonia"){
k<-tx[["Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"]]
l<-k[!is.na(k)]
xtx<- min(as.numeric(l))
ytx<-which(tx[["Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"]] == xtx)
z<-tx[["Hospital.Name"]][ytx]
}
rankdata<-z[order(z)]
rankdata[1]
}