Create Object in Custom Function, Issues with Quoting

24 Views Asked by At

I am trying to calculate a survival function over multiple variables, one of which is fu_time_PJP and fu_status_PJP. Here is the function, which works great under those paramaters:

km_time <- survfit(Surv(time = fu_time_PJP, 
                        event = fu_status_PJP) ~ prophied, 
                   data = df_ptm)

km_time
# Call: survfit(formula = Surv(time = fu_time_PJP, event = fu_status_PJP) ~ 
#     prophied, data = df_ptm)
#              n events median 0.95LCL 0.95UCL
# prophied=0 909      6     NA      NA      NA
# prophied=1 535      4     NA      NA      NA

I would like to write a custom function such that I can pass many variables into the same function, as such, but am getting an error:

km_fun <- function(x) {
  
  y <- paste0("fu_time_", x)
  z <- paste0("fu_status_", x)
  
  # Generate survival object
  km_time <- survfit(Surv(time = y, 
                          event = z) ~ prophied, 
                     data = df_ptm)
} 

km_fun("PJP") 
# Error in Surv(time = y, event = z) : Time variable is not numeric

I think the problem is that my pasted object (y and z) are not being read in the environment or proprly quoted. I've tried everyting, including as.name, as.function, {{}}, enquo, etc.

Help anyone?!?

0

There are 0 best solutions below