I am not sure what you mean by "list of data.frame", as a data-frame structure is already, technically, a list.
All that you require is a single data-frame with your time, outcome, and test variables. There are also various ways of doing what you want. You can use lapply()
at one or more points, but, here, I also use foreach
.
This data is taken from my own tutorial, HERE.
1, look at our data
head(survplotdata)
Time.RFS Distant.RFS gene1 gene2 gene3 gene4 gene5 gene6 gene7 gene8
GSM65752 2280 0 Low Low Low Low Low Low Low Low
GSM65753 2675 0 Low Low Low Low Low Low Low Low
GSM65754 426 1 Low High Low High Low High Low High
GSM65755 182 1 Low Low Low Low Low Low Low Low
GSM65756 46 1 Low High Low High Low High Low High
GSM65757 3952 0 Low High Low High Low High Low High
NB - this data is just 2 genes whose values I have duplicated just for this example
2, create a list of formulae
Here, we can use either foreach
or lapply
:
foreach
formulae <- list()
genes <- colnames(survplotdata)[3:ncol(survplotdata)]
require(foreach)
foreach(i = 1:length(genes)) %do% {
formulae[[i]] <- as.formula(paste0("Surv(Time.RFS, Distant.RFS) ~ ", genes[i]))
}
lapply
formulae <- list()
genes <- colnames(survplotdata)[3:ncol(survplotdata)]
formulae <- lapply(genes, function(x) as.formula(paste0("Surv(Time.RFS, Distant.RFS) ~ ", x)))
3, create a list of survival objects
require(survminer)
fits <- surv_fit(formulae, data = survplotdata)
4, create a list of survival plots
p <- ggsurvplot_list(fits,
data = survplotdata,
risk.table = FALSE,
pval = TRUE,
break.time.by = 500,
ggtheme = theme_minimal(),
risk.table.y.text.col = FALSE,
risk.table.y.text = FALSE)
5, plot all together
arrange_ggsurvplots(p[1:length(p)], print = TRUE, ncol = 4, nrow = 2, title = "Survival plots")
It works! Thanks for breaking down the steps to make it easy to follow.