DS4: Song Shuffle Simulation

2024-02-26

Introduction

In this project, we are simulating a song shuffling program, such as that used by Spotify. (more detail here on the background etc.)

Gathering & Organizing Data

The data for this project comes from… Before importing the data into R, we…

To organize the data in R, we will first need to load the necessary libraries and import my data. I will also look at the data to see if I need to do any additional cleaning.

#load necessary libraries
library(tidyverse)
# import data
df <- read_csv("playlist - data_clean.csv")
## Rows: 144 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): genre, artist, title
## 
## ℹ 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.
#check first few rows of data
head(df)
## # A tibble: 6 × 3
##   genre                artist             title               
##   <chr>                <chr>              <chr>               
## 1 Genre 1 - hiphop/rap Alesso             When I'm Gone       
## 2 Genre 1 - hiphop/rap Aliyah's Interlude It Girl             
## 3 Genre 1 - hiphop/rap Big Sean           Beware              
## 4 Genre 1 - hiphop/rap Clipse             Ma, I don't love her
## 5 Genre 1 - hiphop/rap Common             Be                  
## 6 Genre 1 - hiphop/rap Drake              Nice for what

Looking at the first 6 rows, I think the column titles work and all 3 columns are an accurate type (character). I think I would like my genre column to be a factor type column so that I can categorize by the different Genres. Then I will look at the counts of each genre.

# change variable type to factor
df$genre <- as.factor(df$genre)

# get the counts for each genre
df %>% count(genre)
## # A tibble: 6 × 2
##   genre                        n
##   <fct>                    <int>
## 1 Genre 1 - hiphop/rap        26
## 2 Genre 2 - pop/kpop/Latin    22
## 3 Genre 3 -  house/ska         5
## 4 Genre 4 -  rnb/soul         43
## 5 Genre 5 - alt/indie/folk    34
## 6 Genre 6 - country/rock      14

Now I’m going to save this data frame so I can easily access it again later.

genre_counts <- df %>% count(genre)

Modeling

Calculate Theoretical Probabilities ###Next I need to find the theoretical probabilities of getting each genre by creating a new column in my genre counts table.

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        26 0.181 
## 2 Genre 2 - pop/kpop/Latin    22 0.153 
## 3 Genre 3 -  house/ska         5 0.0347
## 4 Genre 4 -  rnb/soul         43 0.299 
## 5 Genre 5 - alt/indie/folk    34 0.236 
## 6 Genre 6 - country/rock      14 0.0972

I don’t like all those long decimal numbers, so I’m going to round them to 2 decimal places.

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        26  0.18
## 2 Genre 2 - pop/kpop/Latin    22  0.15
## 3 Genre 3 -  house/ska         5  0.03
## 4 Genre 4 -  rnb/soul         43  0.3 
## 5 Genre 5 - alt/indie/folk    34  0.24
## 6 Genre 6 - country/rock      14  0.1

This new table tells me that there is an 18% chance of getting a hiphop/rap song, a 15% chance of getting a pop/kpop/Latin song, a 3% chance of getting a house/ska song, a 30% chance of getting an rnb/soul song, a 24% chance of getting an alt/indie/folk song, and a 10% chance of getting a country/rock song.