Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...