Getting a "! Can't combine" error when running Purrr::map_dfr

36 Views Asked by At

I get the following error when running the following command

  game_odds <- map_dfr(games_eventids, ~ game_odds_func(eventId = .x))
  Error in `dplyr::bind_rows()`:
  ! Can't combine `.51$odds` <data.frame> and `..52$odds` <list>.
  Run `rlang::last_trace()` to see where the error occurred.

I have narrowed down the issue but need some assistance addressing the issue. As you can see, list 51 actually contains data

  [[51]]$odds
                                                              id                game_id                    
  market_name sports_book_name                        name
  1           12253-31516-2024-02-24:betonline:moneyline:anaheim_ducks 12253-31516- 
 2024-02-24                      Moneyline        BetOnline               Anaheim Ducks

Whereas, list 52 is an empty list with no data.

  [[52]]$odds
  list()

Is there syntax that I can add to the map_dfr command that will just skip any lists that do not have any data.

1

There are 1 best solutions below

2
Johan Rosa On BEST ANSWER

You can remove the non data frame elements.

  1. Instead of map_df use map to create game_odds. With this you'll get a list of df's.

  2. Filter to exclude the non data frame alementes

game_odds <- list(
  data.frame(id = 1, x = 0.5),
  data.frame(id = 2, x = 0.8),
  list()
)

game_odds
#> [[1]]
#>   id   x
#> 1  1 0.5
#> 
#> [[2]]
#>   id   x
#> 1  2 0.8
#> 
#> [[3]]
#> list()

game_odds[sapply(game_odds, is.data.frame)] |>
  dplyr::bind_rows()
#>   id   x
#> 1  1 0.5
#> 2  2 0.8

Created on 2024-02-23 with reprex v2.0.2