rankall<- function(outcome, num = "best")
{
library(dplyr)
library(magrittr)
outcome2 <- read.csv("outcome-of-care-measures.csv",
colClasses = "character")
if((outcome %in% c("heart attack", "heart failure",
"pneumonia")) == FALSE) {
stop(print("invalid outcome"))
}
if (outcome == "heart attack") {
colnum <- 11
}
else if (outcome == "heart failure") {
colnum <- 17
}
else {
colnum <- 23
}
outcome2[ ,colnum] <- as.numeric(outcome2[ ,colnum])
outcome2 = outcome2[!is.na(outcome2[,colnum]),]
splited = split(outcome2, outcome2$State)
ans = lapply(splited, function(x, num) {
x = x[order(x[,colnum], x$Hospital.Name),]
if(class(num) == "character") {
if(num == "best") {
return (x$Hospital.Name[1])
}
else if(num == "worst") {
return (x$Hospital.Name[nrow(x)])
}
}
else {
return (x$Hospital.Name[num])
}
}, num) ## --------------------------------------------------------------- hightlight line
#Return data.frame with format
return ( data.frame(hospital=unlist(ans), state=names(ans)) )
}
# example output:
r <- rankall("heart attack", 4)
as.character(subset(r, state == "HI")$hospital)
## [1] "CASTLE MEDICAL CENTER"
head(rankall("heart attack", "worst"))
## hospital state
## AK MAT-SU REGIONAL MEDICAL CENTER AK
## AL HELEN KELLER MEMORIAL HOSPITAL AL
## AR MEDICAL CENTER SOUTH ARKANSAS AR
## AZ VERDE VALLEY MEDICAL CENTER AZ
## CA METHODIST HOSPITAL OF SACRAMENTO CA
## CO NORTH SUBURBAN MEDICAL CENTER CO
Hi, I'm studing this group of code which is for giving the list of hospitals for each of state in US by the input of disease type and ranking. I'm stuck by the num)
in the line of
}, num)
I cannot find the other bracket for the bracket sign on the right side of num
. The code makes sense for me if it doesn't have , num)
This group of code does work, but it doesn't work without , num)
. Can anyone help me figure out why the code has this?