From 73641476283bbb257e598f5d80813e50b977989b Mon Sep 17 00:00:00 2001 From: Tuomas Borman <60338854+TuomasBorman@users.noreply.github.com> Date: Thu, 7 Mar 2024 17:24:19 +0200 Subject: [PATCH] Fix merging of lists (#18) --- R/utils.R | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/R/utils.R b/R/utils.R index d1aa386..74e11b5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -390,12 +390,23 @@ # data.frame #' @importFrom dplyr full_join .full_join_list <- function(res){ - df <- Reduce(function(df1, df2){ - # Get common columns - common_cols <- intersect(colnames(df1), colnames(df2)) - # Merge based on common columns - temp <- full_join(df1, df2, by = common_cols) - return(temp) - }, res) + # Remove empty elements + res <- res[ lengths(res) > 0 ] + # If there is more than one element, merge them + if( length(res) > 1 ){ + df <- Reduce(function(df1, df2){ + # Get common columns + common_cols <- intersect(colnames(df1), colnames(df2)) + # Merge based on common columns + temp <- full_join(df1, df2, by = common_cols) + return(temp) + }, res) + } else if( length(res) == 1 ){ + # Otherwise if there is only one element, give the element + df <- res[[1]] + } else{ + # If all the data.frames were without information, give NULL + df <- NULL + } return(df) }