bind_rows with dataframe empty

Hello,

I am trying to use the bind_rows function to group dataframes.
But I have to check each dataframe because if one of them contains 0 rows, the script fails.

I am thinking of doing it this way:

  • create a function for testing each dataframe like this :
test_df <-  function(df){
  
  if(dim(df)[1]>0){
    test <- TRUE
  }
  else{
    test<- FALSE
  }
  
  return(test)
}
  • use map_dfr (from purrr package) for iterate on each dataframe

I'm trying this code but it doesn't work :frowning:

df <- list(df_moy_G,df_moy_MG,df_moy_I,df_moy_Y,df_moy_V,df_moy_L)

df %>% map_dfr(test_df==TRUE) 

thanks a lot for your help !

This doesn't seem correct to me. Can you check my following examples ?

library(dplyr)
(a_ <- data.frame(x=1))
(b_ <- data.frame(x=1) |> filter(FALSE))

(c_ <- bind_rows(a_,b_))

(a_ <- tibble(x=1))
(b_ <- tibble(x=1) |> filter(FALSE))

(c_ <- bind_rows(a_,b_))

Can you check my following examples ?

indeed, your examples caught my attention !!! because it works without failed.

I have therefore reproduced an example with my data

library(tidyverse)
ordre <- c(1,2,3)
classe <- c("R","A","B")
point <- c(0,-3,-8)
tranche <- c("(0,50000]","(50000,100000]","(100000,1000000]")

df1 <- tibble(ordre,classe,point,tranche)

df2 <- df1 %>% filter(ordre==4) %>% select(-point)

bind_rows(df1,df2)
# A tibble: 3 x 4
  ordre classe point tranche         
  <dbl> <chr>  <dbl> <chr>           
1     1 R          0 (0,50000]       
2     2 A         -3 (50000,100000]  
3     3 B         -8 (100000,1000000]

the problem is therefore elsewhere !!

feel free to attempt a reprex of your real issue.
Otherwise I can advise you to look at the purrr helper functions safely() quietly() possibly()