运行 Purrr::map_dfr 时出现“!无法组合”错误

问题描述 投票:0回答:1

运行以下命令时出现以下错误

  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.

我已经缩小了问题范围,但需要一些帮助来解决该问题。正如你所看到的,列表 51 实际上包含数据

  [[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

而列表 52 是一个空列表,没有数据。

  [[52]]$odds
  list()

是否可以将语法添加到map_dfr命令中,该命令将跳过任何没有任何数据的列表。

r tidyverse purrr
1个回答
0
投票

您可以删除非数据框元素

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[purrr::map_lgl(game_odds, is.data.frame)] |>
  dplyr::bind_rows()
#>   id   x
#> 1  1 0.5
#> 2  2 0.8

创建于 2024-02-23,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.