bind_rows_(x, .id) 中的错误:参数 1 的名称必须在 purrr 中使用 map_df
Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr
我正在使用 spotifyr 包为我的数据集中特定专辑的每首歌曲抓取 spotify 音频特征。我的问题是我的数据集包含一些不在 spotify 上的艺术家——所以他们不应该返回任何值。
我的问题是,当我访问不在 spotify 上的艺术家时,我收到此错误:
Error in bind_rows_(x, .id) : Argument 1 must have names
我尝试将函数包装在 tryCatch 中,以便为有问题的行的每一列获取 NA
,但它似乎不起作用。
这是我的代码示例(仅供参考,您需要从 Spotify 的网站API 访问 运行 Spotifyr 代码)
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
.
这是一个更简单的问题示例。
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
我正在使用 spotifyr 包为我的数据集中特定专辑的每首歌曲抓取 spotify 音频特征。我的问题是我的数据集包含一些不在 spotify 上的艺术家——所以他们不应该返回任何值。
我的问题是,当我访问不在 spotify 上的艺术家时,我收到此错误:
Error in bind_rows_(x, .id) : Argument 1 must have names
我尝试将函数包装在 tryCatch 中,以便为有问题的行的每一列获取 NA
,但它似乎不起作用。
这是我的代码示例(仅供参考,您需要从 Spotify 的网站API 访问 运行 Spotifyr 代码)
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
.
这是一个更简单的问题示例。
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