Search code examples
rapiplumberrjsonmultidplyr

merge multiple table with different length and form a single table in R


i am using plumber api for an api. i have multiple sub-tables in which all table are connected with there primary keys (study_id) and i wanted to merge all table with single primary keys to form a single table. Some tables have different length.

for example :- countries_of_origin_table and countries_of_recruitment_table have different length table

library(plumber)
library(tibble)
library(gwasrapidd)
library(dplyr)

#* @get /Studies 

detailData <- function(query = ""){
  print(query)
  studies <- get_studies(efo_trait = query)
  
  study <- studies@studies
  study
            
  publication <- studies@publications
  publication
  
  genotyping_techs_table <- studies@genotyping_techs
  genotyping_techs_table
  
  platforms_table <- studies@platforms 
  platforms_table
  
  ancestries_table <- studies@ancestries
  ancestries_table
  
  ancestral_groups_table <- studies@ancestral_groups
  ancestral_groups_table
  
  countries_of_origin_table <- studies@countries_of_origin
  countries_of_origin_table
  
  countries_of_recruitment_table <- studies@countries_of_recruitment
  countries_of_recruitment_table
  
  Studies_table = list(study, genotyping_techs_table, platforms_table,
   ancestries_table, ancestral_groups_table, countries_of_recruitment_table,
   countries_of_origin_table ,publication)
 

i have tried this to merge all tables but didn't work

collection <- merge(study, genotyping_techs_table, platforms_table,
                      ancestries_table, ancestral_groups_table, countries_of_recruitment_table,
                      countries_of_origin_table ,publication, by = "study_id" ,all=TRUE)
  
collection

Please Help me

Thank You in Advance


Solution

  • According to ?merge, it allows only two datasets at a time for joining

    merge(x, y, ...)

    where

    x, y - data frames, or objects to be coerced to one.

    An option is to place the datasets in a list and use Reduce to do a sequential join

    lst1 <- list(study, genotyping_techs_table, platforms_table,
                      ancestries_table, ancestral_groups_table, 
             countries_of_recruitment_table,
                      countries_of_origin_table ,publication)
    
    out <- Reduce(function(...) merge(..., by = "study_id" , all = TRUE), lst1)