DS4, Onalee: Song Shuffle Simulation

04-09-2024

Introduction:

In this project, I am simulating a song-shuffling program and determing the probability of a given genre being played in a shuffled playlist made of our class’ favorite songs.

Gathering and Organizing Data:

This data comes from a spreadsheet that cataloged our class’ favorite songs, and those songs’ artists/genres. Before importing the data into R Studios I cleaned it up by correcting spelling mistakes and removing irrelevant data such as the time that a song was submitted to the Google Form we used to compile data.

I downloaded tidyverse after importing a clean .cvs file of my data, and then I run a line of code to display the parts of it that are relevent to the question I’m answering: how likely is it for each given genre to be played?

install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
df<-read_csv("Playlist - Clean.csv")
## Rows: 175 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Genre, Artist, Song
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(df)
## # A tibble: 6 × 3
##   Genre                    Artist         Song                          
##   <chr>                    <chr>          <chr>                         
## 1 Genre 1 - hiphop/rap     8k             When you call                 
## 2 Genre 2 - pop/kpop/Latin Yonaguni       bad bunny                     
## 3 Genre 1 - hiphop/rap     $uicideboy$    Diemonds                      
## 4 Genre 1 - hiphop/rap     $uicideboy$    Not Even Ghosts Are This Empty
## 5 Genre 1 - hiphop/rap     03 Greedo      Trap House                    
## 6 Genre 5 - alt/indie/folk 10,000 Maniacs Trouble Me

On first glance this looks good. Now I’m going to make my genre column a factor type column so that I can categorize by the different Genres. Then I will look at the counts of each genre.

df$Genre <- as.factor(df$Genre)
head(df)
## # A tibble: 6 × 3
##   Genre                    Artist         Song                          
##   <fct>                    <chr>          <chr>                         
## 1 Genre 1 - hiphop/rap     8k             When you call                 
## 2 Genre 2 - pop/kpop/Latin Yonaguni       bad bunny                     
## 3 Genre 1 - hiphop/rap     $uicideboy$    Diemonds                      
## 4 Genre 1 - hiphop/rap     $uicideboy$    Not Even Ghosts Are This Empty
## 5 Genre 1 - hiphop/rap     03 Greedo      Trap House                    
## 6 Genre 5 - alt/indie/folk 10,000 Maniacs Trouble Me
df %>% count(Genre)
## # A tibble: 6 × 2
##   Genre                        n
##   <fct>                    <int>
## 1 Genre 1 - hiphop/rap        58
## 2 Genre 2 - pop/kpop/Latin    25
## 3 Genre 3 -  house/ska         5
## 4 Genre 4 -  rnb/soul         14
## 5 Genre 5 - alt/indie/folk    45
## 6 Genre 6 - country/rock      28

Now I can see how many times each genre occurs. I’m going to save this data, and then I will make it so that I can see the probability of each genre occurring in the form of a decimal percentage.

genre_counts <- df %>% count(Genre)
genre_counts <- genre_counts %>% mutate(prob = n/sum(n))
genre_counts
## # A tibble: 6 × 3
##   Genre                        n   prob
##   <fct>                    <int>  <dbl>
## 1 Genre 1 - hiphop/rap        58 0.331 
## 2 Genre 2 - pop/kpop/Latin    25 0.143 
## 3 Genre 3 -  house/ska         5 0.0286
## 4 Genre 4 -  rnb/soul         14 0.08  
## 5 Genre 5 - alt/indie/folk    45 0.257 
## 6 Genre 6 - country/rock      28 0.16

This is accurate, but it will be easier to read if I round it down to the first two decimals, so I’ll do that.

genre_counts$prob <- round(genre_counts$prob, 2)
genre_counts
## # A tibble: 6 × 3
##   Genre                        n  prob
##   <fct>                    <int> <dbl>
## 1 Genre 1 - hiphop/rap        58  0.33
## 2 Genre 2 - pop/kpop/Latin    25  0.14
## 3 Genre 3 -  house/ska         5  0.03
## 4 Genre 4 -  rnb/soul         14  0.08
## 5 Genre 5 - alt/indie/folk    45  0.26
## 6 Genre 6 - country/rock      28  0.16

In conclusion: this table tells me that there is a 33% chance of getting a Hip-Hop/Latin song, a 14% chance of getting a pop song, a 3% chance of getting a House/EDM song, an 8% chance of getting an R&B/Blues song, a 26% chance of getting an Alt/Indie song, and a 16% chance of getting a country/rock song.