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("Class Playlist Clean DS4 - Sheet1.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.
df$ Genre<-as.factor(df$Genre)
genre_counts<-df %>% count(Genre)
genre_counts
## # 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
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
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
#Calculating Experimental Probabilities ###Our next task is to simulate how shuffle works by pulling a random sample of songs and saving it as a new table
sample_n(df, 10) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 3 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 5 0.5
## 2 Genre 2 - pop/kpop/Latin 2 0.2
## 3 Genre 5 - alt/indie/folk 3 0.3
##Analyze the findings ##The data from the sample somewhat represent the class data. Our classes’ genres are not evenly distributed and so some genres will play more than others. The data in the sample data set shows some similarity with the class data in that Genre 1 is still the highest. However Genre 5 which had the second highest probability of being played in the class data set is one of the least likely in the sample set. ##Experimental and Theoretical probability ##Theoretical probability is how likely an event is to occur, or how likely a certain genre is going to play, but experimental probability is how often it actually happens. However after many trials the experimental probability will start to approach the theoretical probability.
sample_n(df, 20) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 5 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 6 0.3
## 2 Genre 2 - pop/kpop/Latin 2 0.1
## 3 Genre 4 - rnb/soul 2 0.1
## 4 Genre 5 - alt/indie/folk 8 0.4
## 5 Genre 6 - country/rock 2 0.1
sample_n(df, 30) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 6 0.2
## 2 Genre 2 - pop/kpop/Latin 7 0.23
## 3 Genre 3 - house/ska 1 0.03
## 4 Genre 4 - rnb/soul 3 0.1
## 5 Genre 5 - alt/indie/folk 10 0.33
## 6 Genre 6 - country/rock 3 0.1
sample_n(df, 40) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 5 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 15 0.38
## 2 Genre 2 - pop/kpop/Latin 5 0.12
## 3 Genre 4 - rnb/soul 3 0.07
## 4 Genre 5 - alt/indie/folk 9 0.22
## 5 Genre 6 - country/rock 8 0.2
sample_n(df, 100) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 30 0.3
## 2 Genre 2 - pop/kpop/Latin 17 0.17
## 3 Genre 3 - house/ska 2 0.02
## 4 Genre 4 - rnb/soul 8 0.08
## 5 Genre 5 - alt/indie/folk 25 0.25
## 6 Genre 6 - country/rock 18 0.18
sample_n(df, 70) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
## Genre n probability
## <fct> <int> <dbl>
## 1 Genre 1 - hiphop/rap 20 0.29
## 2 Genre 2 - pop/kpop/Latin 6 0.09
## 3 Genre 3 - house/ska 2 0.03
## 4 Genre 4 - rnb/soul 9 0.13
## 5 Genre 5 - alt/indie/folk 19 0.27
## 6 Genre 6 - country/rock 14 0.2
#Analyze & Synthesize ##The genres played on shuffle represent the genres in the class playlist because after shuffeling the playlist so many times the throretical probabilty will start to approach the experimental probability. The genres remain in generally the order that the theoretical probability predicted. The theoretical probability is what is most likley to happen, which may not be what actually happens but will look similar to it eventually. #Reflection ##The most challeging piece of code for me was first getting the probabilities to show up as a new colum. I feel the most proud of the one that rounded the probabilites because I didnt need to get help for it. I’d like to learn about other usefl applications of code in our day to day life.If I could do this again I’d procrastinate less because I was overwhelmed by it.