我有以下数据帧songs
|artistName |trackName |id |duration |
|:------------|:-------------------------|:----------------------|:--------|
|Shaan |Woh Ladki Hai Kahan |2tO0QPdCA0jgDBshTuYYkc |306133 |
|Amit Trivedi |Zinda |1gEHNfJRSXpDVaEicwRRfe |241962 |
|flora cash |You're Somebody Else |0pdKRp2sUthTPe7RLWpPqQ |218883 |
|Iron & Wine |Each Coming Night |2xa9PoO42d7VjT0KqG5d3I |207773 |
|Yasser Desai |Tenu Na Bol Pawaan |5mp7og60TpyNiKi2p9Morw |295352 |
|ERock |Jurassic Park Meets Metal |4u0RJ8g7QM5exHIPvJ0WW3 |244000 |我对id专栏很感兴趣。它由{spotifyr}包中的get_track_audio_features()函数用来返回一个音轨音频特性的数据帧。例如,一个id可能返回:
| danceability| energy| key| loudness| mode| speechiness| acousticness| instrumentalness| liveness| valence| tempo|type |id |uri |track_href |analysis_url | duration_ms| time_signature|
|------------:|------:|---:|--------:|----:|-----------:|------------:|----------------:|--------:|-------:|------:|:--------------|:----------------------|:------------------------------------|:--------------------------------------------------------|:----------------------------------------------------------------|-----------:|--------------:|
| 0.47| 0.237| 2| -15.593| 1| 0.0348| 0.837| 0.464| 0.115| 0.358| 81.268|audio_features |2xa9PoO42d7VjT0KqG5d3I |spotify:track:2xa9PoO42d7VjT0KqG5d3I |https://api.spotify.com/v1/tracks/2xa9PoO42d7VjT0KqG5d3I |https://api.spotify.com/v1/audio-analysis/2xa9PoO42d7VjT0KqG5d3I | 207773| 4|我想做的是添加某些音频功能,例如energy和danceability到songs中,对应于它们的is。生成的数据帧变成:
| id | artistName | trackName | duration | energy | danceability |
|------------------------|--------------|----------------------|----------|--------|--------------|
| 0pdKRp2sUthTPe7RLWpPqQ | flora cash | You're Somebody Else | 2349023 | .47 | .13 |
| 1gEHNfJRSXpDVaEicwRRfe | Amit Trivedi | Zinda | 234009 | .15 | .78 |我想要使用rowwise()函数来实现这一点,并将所有内容都变成一个单独的列,然后将其分开(因为它们将用逗号分隔,并且可以变成新的列):
song_data <- songs %>%
rowwise() %>%
mutate(song_data_id = toString(get_track_audio_features(id)) %>%
separate(.,
song_data_id,
into = c("danceablity", "energy"),
sep = ",")我认为这是可行的,因为函数返回的前两列是danceability和energy,其余的将被丢弃。但是,它并没有这样做,而是给出了以下错误:
cannot coerce type 'closure' to vector of type 'character'如何才能以最短的方式将多个列变异到我的数据帧中?
Dput:
structure(list(endTime = structure(c(1588582680, 1588640040,
1588640040, 1588640280, 1588640280, 1588641240), class = c("POSIXct",
"POSIXt"), tzone = ""), artistName = c("Shaan", "Amit Trivedi",
"flora cash", "Amit Trivedi", "flora cash", "flora cash"), trackName = c("Woh Ladki Hai Kahan",
"Zinda", "You're Somebody Else", "Zinda", "You're Somebody Else",
"You're Somebody Else"), msPlayed = c(17253L, 0L, 218649L, 1504L,
218883L, 638496L), date = structure(c(18386, 18387, 18387, 18387,
18387, 18387), class = "Date"), time = structure(c(52080, 23040,
23040, 23280, 23280, 24240), class = c("hms", "difftime"), units = "secs")), row.names = c(NA,
6L), class = "data.frame")发布于 2021-05-09 02:56:24
由于该函数返回一个数据帧,因此我们可以使用{purrr}中的map函数将其作为列表存储到列中。map()总是返回一个列表。
features <- song_data %>%
nest(data = c(id)) %>%
mutate(audio_features = map(data, ~ get_track_audio_features(.$id))) %>%
unnest(., audio_features) 然后,音频特征被存储在一个名为audio_features的列中,但这仍然是一个列表。最后一行,unnest()函数将列表展开为单独的列。全都做完了!
发布于 2021-05-09 00:51:50
如果您将id添加到get_track_audio_features()的结果中,则可以使用join组合表,例如left_join或inner_join。
https://stackoverflow.com/questions/67448928
复制相似问题