Sometimes, after I wrote a function and called it, the result in the R studio console is the following:
> rankhospital("NC", "heart attack", "best")
Nothing is returned, and no error occured.
Code:
rankhospital<-function(state, outcome, num="best")
{
a<-read.csv("outcome-of-care-measures.csv",na.string="Not Available")
b<-subset(a,a[,7]==state)
if(outcome=="heart attack")
{
conv<-as.numeric(b[,11])
min_num<-min(conv,na.rm = TRUE)
min_row<-which(b[,11]==min_num)
hosname<-b[min_row,2]
hosname
}
if(outcome=="heart failure")
{
conv1<-as.numeric(b[,17])
min_num1<-min(conv1,na.rm = TRUE)
min_row1<-which(b[,17]==min_num1)
hosname1<-b[min_row1,2]
hosname1
}
if(outcome=="heart pneumonia")
{
conv2<-as.numeric(b[,23])
min_num2<-min(conv2,na.rm = TRUE)
min_row2<-which(b[,23]==min_num2)
hosname2<-b[min_row2,2]
hosname2
}
}
The code is very simple. After user input state [,7] and outcome [,11],[,17],[,23], then to find the minimum outcome value for the state's outcome , then return the hospital name [,2] for the minimum outcome.
Which part is wrong? Thanks!
You can return values using return(hosname)
, return(hosname1)
, return(hosname2)
.
rankhospital<-function(state, outcome, num="best")
{
a<-read.csv("outcome-of-care-measures.csv",na.string="Not Available")
b<-subset(a,a[,7]==state)
if(outcome=="heart attack")
{
conv<-as.numeric(b[,11])
min_num<-min(conv,na.rm = TRUE)
min_row<-which(b[,11]==min_num)
hosname<-b[min_row,2]
return(hosname)
}
if(outcome=="heart failure")
{
conv1<-as.numeric(b[,17])
min_num1<-min(conv1,na.rm = TRUE)
min_row1<-which(b[,17]==min_num1)
hosname1<-b[min_row1,2]
return(hosname1)
}
if(outcome=="heart pneumonia")
{
conv2<-as.numeric(b[,23])
min_num2<-min(conv2,na.rm = TRUE)
min_row2<-which(b[,23]==min_num2)
hosname2<-b[min_row2,2]
return(hosname2)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.