1

I'm trying to go through a list of artists and albums, and get the audio features of each song of each album into a data frame (using spotifyr package). However, in my list, there are some misspellings of the album titles, so I'm trying to use agrep or agrepl to fuzzy match

For example:

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

Artist <- c("Eminem", "Spiritualized")
Album <- c("Revival", "Pure Phase")
mydata <- data_frame(Artist, Album)

The get_artist_audio_features() function from spotifyr gives back the audio features for every song from the artist, but I only want songs from some specific album.

get_album_data <- function(x) {
  get_artist_audio_features(mydata$Artist[x]) %>%
    filter(agrepl(album_name, mydata$Album[x]) == TRUE)}

try_get_album_data <- function(x) {
  tryCatch(get_album_data(x), error = function(e) {data.frame()})}

map_df(seq(1,2), try_get_album_data)

When I run this, I get the error:

...argument 'pattern' has length > 1 and only the first element will be used

Any ideas? I know I need to loop through agrepl somehow, but I'm not sure how to implement that within the filter() function, or if I even need filter().

Evan O.
  • 1,553
  • 2
  • 11
  • 20
  • you can transform your vector of name in a regular expression with `paste(album_name,sep="|")` in the grep it will check name1 or name2 or name 3... – s.brunel Feb 02 '18 at 08:12
  • So like this? `filter(agrepl(paste(album_name, sep = "|"), mydata$Album[x]) == TRUE)` Because that still gives me the same warning as before – Evan O. Feb 02 '18 at 14:41
  • i have no warning when i try – s.brunel Feb 02 '18 at 15:30

1 Answers1

1

It turns out that agrepl takes the pattern first and the vector second, so in this case, it should be:

filter(agrepl(mydata$Album[x], album_name) == TRUE)}

rather than

filter(agrepl(album_name, mydata$Album[x]) == TRUE)}

Evan O.
  • 1,553
  • 2
  • 11
  • 20