-1
 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?

  • 1
    Does this answer your question? [R apply function with multiple parameters](https://stackoverflow.com/questions/6827299/r-apply-function-with-multiple-parameters) – divibisan Jan 13 '22 at 16:10
  • 1
    This is part of the `lapply` function. `lapply` loops through the first variable (here `splited`), passing each element to the first argument of the provided function. In this case, the function takes a second argument too. You can give that by passing it to lapply after as an optional argument (the `...` shown in the help). [This answer also shows a nice example](https://stackoverflow.com/a/28102021/8366499) – divibisan Jan 13 '22 at 16:12
  • @divibisan, Thank you so much, I think I have found the answer by your help. Your answer and the links you shared explain the question very well. Sorry that I got missed about trace of bracket, which makes me not realize that `num` is the second argument of applied function in `lapply()`. – Yuliang Tan Jan 14 '22 at 04:23
  • @user2554330, Hi, I tested the code, with and without `num`, and it doesn't work if the code dropped the `num`. But still thank you for your answer. – Yuliang Tan Jan 14 '22 at 04:25

1 Answers1

0

By the help of @divibisan,@user2554330, I recognized the essence of my question, thank you so much. Additionally, I just found there is a post asking exactly the same question as mine, in a more simplified way: passing several arguments to FUN of lapply (and others *apply).
It is well explained by the following answers. I choose to leave this post there in case someone is doing the same assignment and could meet the same confusion like me.