I have a dataset comprised of clinics with each clinic is comprised of doctors, performing procedures on patients.
I have written to perform analyses on the dataset filtering for clinic lists or doctor lists (a simple one is below):
num.of <- function(x.doctor, x.clinic){
if (!missing(x.clinic)){
df_filter <- filter(df_clean, clinic == x.clinic)
}
if (!missing(x.doctor)) {
df_filter <- filter(df_clean, doctor == x.doctor)
}
num_doctor <- length(unique(df_filter$doctor))
num_surveys <- nrow(df_filter)
num_procedure <- length(unique(df_filter$PPID))
result <- setNames(c(num_doctor, num_surveys, num_procedure), c("num_doctor", "num_surveys", "num_procedure"))
return(result)
}
I am attempting to call on these functions with either a list of doctors or a list of clinics:
sapply(doctor_list, num.of, x.clinic = NULL)
However, the function only works when the 'first' argument is passed through, i.e. the function above does not work, but this does:
sapply(clinic_list, num.of, x.doctor = NULL)
If the arguments are reversed when writing the initial function, the opposite of the above examples is true.
The functions are fed only one set of arguments at a time: Either a list for x.doctor or a list for x.clinic.
How can I rewrite my functions please so that apply works x.clinic and in a separate function call for x.doctor?
Thank you!