bind_rows_(x,.id)中的错误:参数1必须在purrr中使用map_df进行命名 [英] Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr

查看:187
本文介绍了bind_rows_(x,.id)中的错误:参数1必须在purrr中使用map_df进行命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spotifyr软件包为数据集中特定专辑的每首歌曲抓取Spotify音频功能。我的问题是我的数据集包含一些不在Spotify上的艺术家-因此他们不应该返回任何值。

I'm using the spotifyr package to scrape spotify audio features for every song of specific albums in my dataset. My issue is that my dataset consists of some artists that are not on spotify -- so they shouldn't be returning any values.

我的问题是,当我遇到不在Spotify上的艺术家时,出现了以下错误:

My issue is that when I get to an artist that is not on spotify, I get this error:

Error in bind_rows_(x, .id) : Argument 1 must have names

我尝试将函数包装在tryCatch中,以为有问题的行的每一列获取 NA ,但似乎不起作用。

I've tried wrapping the function in tryCatch to get NA for each column of the problematic row, but it doesn't seem to work.

这是我的代码示例(仅供参考,您需要从spotify的网站获取API访问权限才能运行spotifyr代码)

Here's an example of my code (FYI, you need to get API access from spotify's website to run the spotifyr code)

library(readr)
library(spotifyr)
library(dplyr)
library(purrr)

Sys.setenv(SPOTIFY_CLIENT_ID = "xxx") #xxx will be from spotify's website
Sys.setenv(SPOTIFY_CLIENT_SECRET = "xxx")
access_token <- get_spotify_access_token()

artist <- c("Eminem", "Chris Stapleton", "Brockhampton", "Big Sean, Metro Boomin")
album <- c("Revival", "From A Room: Volume 2", "SATURATION III", "Double or Nothing")
mydata <- data_frame(artist, album)

get_album_data <- function(x) {
  get_artist_audio_features(mydata$artist[x], return_closest_artist = TRUE) %>%
    filter(album_name == mydata$album[x])}

try_get_album_data <- function(x) {
  tryCatch(get_album_data(x), error = function(e) {NA})}

map_df(seq(1, 4), try_get_album_data)


推荐答案

问题是,当它绑定行时,它不能与 NA 绑定。要解决此问题,只需使用 data.frame()而不是 NA

The problem is that when it binds the rows, it cannot bind with NA. To fix this just use data.frame() rather than NA.

这是问题的一个简单示例。

Here's a simpler example of the problem.

library('dplyr')
library('purrr')

try_filter <- function(df) {
  tryCatch(
    df %>%
      filter(Sepal.Length == 4.6),
    error = function(e) NA)
}

map_df(
  list(iris, NA, iris),
  try_filter)
#> Error in bind_rows_(x, .id) : Argument 1 must have names

解决方案是替换 NA data.frame()

try_filter <- function(df) {
  tryCatch(
    df %>%
      filter(Sepal.Length == 4.6),
    error = function(e) data.frame())
}

map_df(
  list(iris, NA, iris),
  try_filter)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          4.6         3.1          1.5         0.2  setosa
#> 2          4.6         3.4          1.4         0.3  setosa
#> 3          4.6         3.6          1.0         0.2  setosa
#> 4          4.6         3.2          1.4         0.2  setosa
#> 5          4.6         3.1          1.5         0.2  setosa
#> 6          4.6         3.4          1.4         0.3  setosa
#> 7          4.6         3.6          1.0         0.2  setosa
#> 8          4.6         3.2          1.4         0.2  setosa

这篇关于bind_rows_(x,.id)中的错误:参数1必须在purrr中使用map_df进行命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆