Nick Knauer
Nick Knauer

Reputation: 4243

For Loop reading API response with existing Data Frame

I have a dataframe:

df

                        NAME      ARTISTNAME                    COL3
1 Everything_Now (continued)     Arcade Fire Everything_Now%20(continued)%20Arcade%20Fire
2             Everything Now     Arcade Fire             Everything%20Now%20Arcade%20Fire
3              Signs of Life     Arcade Fire            Signs%20of%20Life%20Arcade%20Fire
4           Creature Comfort     Arcade Fire           Creature%20Comfort%20Arcade%20Fire
5                  Peter Pan     Arcade Fire                  Peter%20Pan%20Arcade%20Fire
6                  Chemistry     Arcade Fire                    Chemistry%20Arcade%20Fire

My goal is to loop this with Genius Lyric's API to get the lyric url for each value in COL3.

If I were to not loop this and just do it for each song individually, then my output for one would look like this:

genius_url <- "https://api.genius.com/search?q=Everything_Now%20(continued)%20Arcade%20Fire"
getgeniuslyrics <- GET(genius_url, add_headers(Authorization = HeaderValue))
geniuslyrics <- jsonlite::fromJSON(toJSON(content(getgeniuslyrics)))
answer <- data.frame(geniuslyrics$response$hits$result$url[1])
answer

  X.https...genius.com.Arcade.fire.everything.now.continued.lyrics.
1    https://genius.com/Arcade-fire-everything-now-continued-lyrics 

str(answer)

'data.frame':   1 obs. of  1 variable:
$ X.https...genius.com.Arcade.fire.everything.now.continued.lyrics.: Factor w/ 1 level "https://genius.com/Arcade-fire-everything-now-continued-lyrics": 1

This was my attempt at the for-loop so far but I am getting an error:

for(i in 1:length(df[,3])) {
  genius_url <- paste("https://api.genius.com/search?q=", 
                      df3[i,3], 
                      sep="")
  getgeniuslyrics <- GET(genius_url, add_headers(Authorization = HeaderValue))
  geniuslyrics <- jsonlite::fromJSON(toJSON(content(getgeniuslyrics)))
  answer <- data.frame(geniuslyrics$response$hits$result$url[1])
  df[i,4] <- answer[1,]
}

The error message I am getting is:

Error in x[...] <- m : replacement has length zero
In addition: There were 26 warnings (use warnings() to see them)

Hope this makes sense. Any help would be great, thanks.

Upvotes: 0

Views: 129

Answers (1)

Onyambu
Onyambu

Reputation: 79288

Does your dataframe already have column three or you are to create it from columns 1 and 2? I assumed you have to create the third column given the first and the second.

Try rewriting the one trial in a function like format:

 funfun <- function(...){
  x=unlist(list(...))
  A=paste(unlist(lapply(x,strsplit," ")),collapse = "%20")
  genius_url=paste0("https://api.genius.com/search?q=",A)
  getgeniuslyrics <- GET(genius_url, add_headers(Authorization = HeaderValue))
  geniuslyrics <- jsonlite::fromJSON(toJSON(content(getgeniuslyrics)))
  answer <- data.frame(geniuslyrics$response$hits$result$url[1])
  answer
  }

nom maybe from here you can loop or use apply functions:

  apply(df[,1:2],1,funfun)

in the case you have the third column, then your life is easier:

  funfun_1 <- function(x){
    genius_url=paste0("https://api.genius.com/search?q=",x)
    getgeniuslyrics <- GET(genius_url, add_headers(Authorization = HeaderValue))
    geniuslyrics <- jsonlite::fromJSON(toJSON(content(getgeniuslyrics)))
    answer <- data.frame(geniuslyrics$response$hits$result$url[1])
    answer
  }
sapply(df[,3],funfun_1)

Upvotes: 1

Related Questions