Search code examples
rdplyrtibblesurvival

Object '.' not found while piping with dplyr


I am trying to conduct a survival curve using the survival package. The MWE code is as follows:

df %>% 
  filter(fac <= "Limit") %>% 
  survfit(Surv(tte, !is.na(event)) ~ fac, data = .) %>% 
  ggsurvplot(fit = .)

I get the error Error in eval(fit$call$data) : object '.' not found

When I try to break this down further by:

 survfit <- df %>% 
  filter(fac <= "Limit") %>% 
  survfit(Surv(tte, !is.na(event)) ~ fac, data = .)

 ggsurvplot(fit = survfit)

I get an identical error. Is anyone able to figure out how to pipe from my dataframe all the way through a survival curve? The reason I would like to do this is to streamline the filtering of my dataframe in order to produce a multitude of different survival curves without having to create many subsetted dataframes.


Solution

  • Apparently, ggsurvplot expects an object of class "survfit" as its first argument but also needs the data set as an argument.

    The example below is based on the first example of function
    survfit.formula {survival}.

    library(dplyr)
    library(survival)
    library(survminer)
    
    aml %>%
      survfit(Surv(time, status) ~ x, data = .) %>%
      ggsurvplot(data = aml)
    

    enter image description here

    In the question's case this would become

    df %>% 
      filter(fac <= "Limit") %>% 
      survfit(Surv(tte, !is.na(event)) ~ fac, data = .) %>% 
      ggsurvplot(data = filter(df, fac <= "Limit"))