Passing dataframe column name to custom function that uses survival::Surv

46 views Asked by At

I'm getting the error Error in Surv(tte, eventfl) : object 'pfstte' not found, which is because it's not evaluating custom function's (hrdf) input parameters because they are column names of the indf dataframe. How do I pass a dataframe's column names into a custom function? I tried {{}}, [[]], eval(), and substitute, but none of these worked. Thanks!

hrdf <- function(indf, tte, eventfl) {
  
  df <- broom::tidy(coxph(Surv(tte, eventfl) ~ ENRLARM_STD, data = indf[indf$SEX == 'Female',], ties = 'efron'), conf.int = T) %>%
    dplyr::mutate(term = 'Sex', cat = 'Female')
  return(df)
}
1

There are 1 answers

2
razorofockham On

Try replacing the Surv(tte, eventfl) portion by the following:

Surv(eval(parse(text = tte)), eval(parse(text = eventfl)))

I believe that should do the trick if you then pass the inputs tte and eventfl to your function hrdf() as strings.