简体   繁体   English

将字符向量套回到原始数据帧

[英]Lapply character vector back to original dataframe

I am using the lapply function to call Spotify's API. 我正在使用lapply函数来调用Spotify的API。 I have a column of albumids and I want to retrieve the track names from each albumid. 我有一列专辑,我想从每个专辑中检索曲目名称。 The way I did this was by separating the albumid into a character vector and then running and using the lapply function to call each value in the character vector. 我这样做的方法是将白蛋白分离为一个字符向量,然后运行并使用lapply函数调用字符向量中的每个值。 The problem with this is that I cannot put this back into the original dataframe. 问题是我无法将其放回原始数据框中。

Here is an example: 这是一个例子:

I have a dataframe that looks like this: 我有一个看起来像这样的数据框:

df
               Artist ID Artist Name     Album Name              Album IDs
1 5lDUVqxXYEOAf12p0N2kVT Jared Dylan       We Can't 16PsUE4xzqxIvmNIlwQzmK
2 5lDUVqxXYEOAf12p0N2kVT Jared Dylan Love Is a Game 4NYvHAON7Zo4KC2BMn5rlI
3 5lDUVqxXYEOAf12p0N2kVT Jared Dylan Enjoy the View 5AipKv8MFdERzk9xxEN3OK
4 5lDUVqxXYEOAf12p0N2kVT Jared Dylan  Luna Loves Me 3u4XXqFOiltaNXyKAhkzGN
5 5lDUVqxXYEOAf12p0N2kVT Jared Dylan       Symphony 0x5PQLx5tFNEpfhQnXhU8t
6 5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g

I then created a character vector from the Album IDs column like below: 然后,从“专辑ID”列创建一个字符向量,如下所示:

spotify<-df$`Album IDs`

Then I created a function to retrieve the trackids, artistid, artist name, and track names for each Album ID: 然后,我创建了一个函数来检索每个专辑ID的trackid,artistid,艺术家名称和曲目名称:

get.tracks <- function(spotify){
  albumTracksURL <- paste("https://api.spotify.com/v1/albums/", spotify, "/tracks?limit=50", sep="")
  getTracks <- GET(albumTracksURL, add_headers(Authorization = HeaderValue))
  albumTracks <- jsonlite::fromJSON(toJSON(content(getTracks)))

  ids <- data.frame(matrix(unlist(albumTracks$items$id), 
                       nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)

  names <- data.frame(matrix(unlist(albumTracks$items$name), 
                         nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
  artists<-albumTracks$items$artists
  artists1<-do.call(rbind, lapply(artists, function(x) do.call(cbind, lapply(x[c('id', 'name')], toString))))

  result <- cbind(ids, names, artists1)

  colnames(result) <- c("ID", "NAME", "ARTIST ID", "ARTIST NAME")

  return(result)
}

df <- lapply(spotify, get.tracks)

result <- do.call(rbind, df)
result_final<-result

Here is what this result looks like: 结果如下所示:

result_final

                       ID             NAME              ARTIST ID ARTIST NAME
1  2YjOdgzqMIokknjOLS9ksc         We Can't 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
2  5zSJepyr4V94yICIwhEBNK   Love Is a Game 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
3  4o7qeWp4eQflplPVtoiJhM   Enjoy the View 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
4  4LtSJDlu8UvM4hNlJsSj31    Luna Loves Me 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
5  4a91BtkczDVQdgKWUiwuyy         Symphony 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
6  36XHtu9IgcoUEylPDYfPpr         In Panic 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
7  6LEgJv1ClgvjDzmr7WG4hE         Mistakes 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
8  5GxzbBprsHtk05qQeATGZR Ready, Set, Stun 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
9  1PHIPb4HyaatPqQfw2t288   Drive Me Crazy 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
10 6M9hgVrs4oj4YCFdXod1u4  Always Faithful 5lDUVqxXYEOAf12p0N2kVT Jared Dylan

The problem with this is that I cannot relate it back to the original dataframe since there is no primary key (albumid). 问题在于,由于没有主键(相册),因此我无法将其与原始数据帧相关联。 I think I need to do a for-loop or a different apply function on the original df instead of separating it out as a character vector but I do not know how to do that. 我想我需要在原始df上执行for循环或其他Apply函数,而不是将其分离为字符向量,但是我不知道该怎么做。 The goal is to make the final result look like this below: 目的是使最终结果如下所示:

                Artist.ID Artist.Name     Album.Name              Album.IDs                     ID             NAME              ARTIST.ID ARTIST.NAME
1  5lDUVqxXYEOAf12p0N2kVT Jared Dylan       We Can't 16PsUE4xzqxIvmNIlwQzmK 2YjOdgzqMIokknjOLS9ksc         We Can't 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
2  5lDUVqxXYEOAf12p0N2kVT Jared Dylan Love Is a Game 4NYvHAON7Zo4KC2BMn5rlI 5zSJepyr4V94yICIwhEBNK   Love Is a Game 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
3  5lDUVqxXYEOAf12p0N2kVT Jared Dylan Enjoy the View 5AipKv8MFdERzk9xxEN3OK 4o7qeWp4eQflplPVtoiJhM   Enjoy the View 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
4  5lDUVqxXYEOAf12p0N2kVT Jared Dylan  Luna Loves Me 3u4XXqFOiltaNXyKAhkzGN 4LtSJDlu8UvM4hNlJsSj31    Luna Loves Me 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
5  5lDUVqxXYEOAf12p0N2kVT Jared Dylan       Symphony 0x5PQLx5tFNEpfhQnXhU8t 4a91BtkczDVQdgKWUiwuyy         Symphony 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
6  5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g 36XHtu9IgcoUEylPDYfPpr         In Panic 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
7  5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g 6LEgJv1ClgvjDzmr7WG4hE         Mistakes 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
8  5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g 5GxzbBprsHtk05qQeATGZR Ready, Set, Stun 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
9  5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g 1PHIPb4HyaatPqQfw2t288   Drive Me Crazy 5lDUVqxXYEOAf12p0N2kVT Jared Dylan
10 5lDUVqxXYEOAf12p0N2kVT Jared Dylan    In Panic EP 02mIkCCHDYzJWDzh0DDs5g 6M9hgVrs4oj4YCFdXod1u4  Always Faithful 5lDUVqxXYEOAf12p0N2kVT Jared Dylan

At the end of your function get.tracks add a column for album. 在函数的结尾, get.tracks为专辑添加一列。

...
colnames(result) <- c("ID", "NAME", "ARTIST ID", "ARTIST NAME")
result$`Album IDs` <- spotify
return(result)

Then you can merge back into the original using Album IDs as the key. 然后,您可以使用Album IDs作为键合并回原始文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM