From 9e0fc6106fff5d1d8974a8487a75268fd141df36 Mon Sep 17 00:00:00 2001 From: Valerio Bartolino Date: Wed, 28 May 2025 12:06:16 +0200 Subject: [PATCH] Update g3_fit_combine.R bind all the elements of multiple gadget.fit objects with one function --- R/g3_fit_combine.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/R/g3_fit_combine.R b/R/g3_fit_combine.R index 7ab8e7f..4c2cb55 100644 --- a/R/g3_fit_combine.R +++ b/R/g3_fit_combine.R @@ -23,3 +23,18 @@ bind_fit_components <- function(fit_list, component){ return(out) } + +#' this is a possible generalisation to bind all the elements of multiple gadget.fit objects +bind_fit <- function(fit_list, ...){ + # initialise an empty list + out <- fit_list[[1]] + out[] <- NA + for(i in 1:length(out)){ + if(sum(class(fit_list[[1]][[names(out)[i]]]) == "data.frame") == 1){ + tmp <- sapply(fit_list,function(x){x[names(out)[i]]}) + names(tmp) <- names(fit_list) # replace with readable model names + out[[i]] <- bind_rows(tmp, .id="id") + } else { NULL }} + + return(out) +}