Home > Software design >  How do I simplify this code in R? Determine the most popular genre and calculate the total number of
How do I simplify this code in R? Determine the most popular genre and calculate the total number of

Time:10-17

I have a music data in R and I have to group the data by genre, calculate the total number of streams within each genre, and sort the result to show the most popular genre first.

I have written this code, but I feel like I repeat some of the steps when sorting the result to show the most popular genre. Could you please help me to simplify this code?

Thank you!

#Group the data by genre and calculate the total number of streams
genre <- aggregate(music_data$streams, by = list(music_data$genre), FUN = sum)

#Change the name of the columns
colnames(genre) <- c("genre", "streams")

#Sort the result to show the most popular genre first
genre_sorted <- arrange(genre, desc(streams))
rownames(genre_sorted) <- genre_sorted$genre
genre_grouped_sorted <- subset(genre_sorted, select = c("streams"))
genre_sorted

CodePudding user response:

It can be done in tidyverse as

library(dplyr)
library(tibble)
music_data %>%
    group_by(genre) %>%
    summarise(streams = sum(streams, na.rm = TRUE), .groups = 'drop') %>%
    arrange(genre, desc(streams)) %>%
    column_to_rownames('genre')
  •  Tags:  
  • r
  • Related