Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

dplyr - Subsetting values even further in r

I am having trouble trying to list all the songs in the 5 genres for this question. I have narrowed down the top 5 genres now I am having issues trying to find all the songs in them.

spotify%>%
  group_by(?..genre)%>%
  summarise(Mean0 = mean(popularity))%>%
  arrange(Mean0,desc())%>%
  top_n(5)%>%
  select(?..genre)

Not sure if there is an easier way to come at this question.

The question asks to create a subset of Spotify data by selecting all the tracks with the top five most popular genres.Call the subset as spotify_s and print the tibble spotify_s.

The variables include

?..genre,
artist_name,
track_name,
track_id,
popularity,
acousticness,
danceability,
duration_ms,
energy,
instrumentalness,
key,
liveness,
loudness,
mode,
speechiness,
tempo,
time_signature,
valence

I can provide more information if needed

question from:https://stackoverflow.com/questions/65912172/subsetting-values-even-further-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the following :

library(dplyr)

spotify_s <- spotify %>%
               group_by(?..genre) %>%
               summarise(Mean0 = mean(popularity)) %>%
               top_n(5, Mean0) %>%
               select(?..genre) %>%
               left_join(spotify, by = '?..genre')

Select top 5 genre based on their average popularity, using left_join we then keep all the rows for those 5 genre.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...