#In this project each member of our class added 5-10 songs to create a class playlist. This is to look at different preferences of certain genres. The question I have for the project is: “What is the probability of a specific genre getting played?”

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ 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
playlist_data<-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.
head(playlist_data)
## # 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

#To calculate the probability you would need to find how many songs are in each genre, next you divide the amount for say Genre 1 and divide it by the amount of rows which is 144, then multiply by 100 to get the percentage.

#I am going to create a data table with all the numbers of how many songs per genre. Looking through the R Cheat Sheet I searched the word “count”. This pulled up the code: count( df$column1).

#Next I am going to need to divide the numbers of songs per genre by 144 because this will give me the number that I will need to multiply by 100 to get the percentage for each genre. I looked in the R Cheat Sheet for a way to make calculations. I found the code: mutate( newcol = operations), you can use this to create a new column as well as calculate a percentage.

playlist_data_summary<-count(playlist_data, genre)
head(playlist_data_summary)
## # A tibble: 6 × 2
##   genre                        n
##   <chr>                    <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

#The data shows that rmb/soul was most liked coming in first with 43 songs, alt/indie/folk came in second with 34, hiphop/rap came in third with 26, pop/kopo/latin came in fourth at 22, country/ rock came in fifth at 14 and coming in last was house/ska which was 5 songs.

select(playlist_data_summary, n)
## # A tibble: 6 × 1
##       n
##   <int>
## 1    26
## 2    22
## 3     5
## 4    43
## 5    34
## 6    14
playlist_data_summary<-mutate(playlist_data_summary, theoretical_probability = n/144)
playlist_data_summary
## # A tibble: 6 × 3
##   genre                        n theoretical_probability
##   <chr>                    <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
playlist_data_summary<-mutate(playlist_data_summary, percentage = theoretical_probability*100)
playlist_data_summary
## # A tibble: 6 × 4
##   genre                        n theoretical_probability percentage
##   <chr>                    <int>                   <dbl>      <dbl>
## 1 Genre 1 - hiphop/rap        26                  0.181       18.1 
## 2 Genre 2 - pop/kpop/Latin    22                  0.153       15.3 
## 3 Genre 3 -  house/ska         5                  0.0347       3.47
## 4 Genre 4 -  rnb/soul         43                  0.299       29.9 
## 5 Genre 5 - alt/indie/folk    34                  0.236       23.6 
## 6 Genre 6 - country/rock      14                  0.0972       9.72

#Now I have gotten the percentages by dividing the column “n” by 144. Then I turned that data into a new column called “theoretical_probability” then proceeded to multiply that column by 100 to get the percentage. I put that data into a new column called “percentage.”

#Genre 1 is 18%, Genre 2 is 15.3%, Genre 3 is 3.5%, Genre 4 is 29.9%, Genre 5 is 23.6% & Genre 6 is 9.7%

#Next I am going to generate 10 random songs with the code sample_n(df, n), using this code ( as well as the mutate code) I can see if the percentages I found above make sense.

sample_n(playlist_data, 10)
## # A tibble: 10 × 3
##    genre                    artist               title                         
##    <chr>                    <chr>                <chr>                         
##  1 Genre 5 - alt/indie/folk Faye Webster         A Dream With A Baseball Player
##  2 Genre 5 - alt/indie/folk Nirvana              About A Girl                  
##  3 Genre 5 - alt/indie/folk the strokes          call it fate, call it karma   
##  4 Genre 5 - alt/indie/folk alkaline trio        Scars                         
##  5 Genre 5 - alt/indie/folk Lake Street Dive     Hypotheticals                 
##  6 Genre 1 - hiphop/rap     Aliyah's Interlude   It Girl                       
##  7 Genre 3 -  house/ska     Tobias Dray          Sunsets                       
##  8 Genre 5 - alt/indie/folk Cigarettes after sex Sunsetz                       
##  9 Genre 2 - pop/kpop/Latin ariana grande        everytime                     
## 10 Genre 5 - alt/indie/folk wilco                heavy metal drummer
sample_data<-sample_n(playlist_data, 10)
head(sample_data)
## # A tibble: 6 × 3
##   genre                    artist              title             
##   <chr>                    <chr>               <chr>             
## 1 Genre 5 - alt/indie/folk my bloody valentine To Here Knows When
## 2 Genre 6 - country/rock   Citizen             Jet               
## 3 Genre 6 - country/rock   Dolly parton        Jolene            
## 4 Genre 4 -  rnb/soul      sza                 snooze            
## 5 Genre 1 - hiphop/rap     Rod Wave            Get Ready         
## 6 Genre 5 - alt/indie/folk Taylor Swift        Peace
sample_data<-count(sample_data, genre)
head(sample_data)
## # A tibble: 4 × 2
##   genre                        n
##   <chr>                    <int>
## 1 Genre 1 - hiphop/rap         2
## 2 Genre 4 -  rnb/soul          4
## 3 Genre 5 - alt/indie/folk     2
## 4 Genre 6 - country/rock       2
sample_data<-mutate(sample_data, probability = n/10)
sample_data
## # A tibble: 4 × 3
##   genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         2         0.2
## 2 Genre 4 -  rnb/soul          4         0.4
## 3 Genre 5 - alt/indie/folk     2         0.2
## 4 Genre 6 - country/rock       2         0.2
sample_data<-mutate(sample_data, percentage = probability*100)
sample_data
## # A tibble: 4 × 4
##   genre                        n probability percentage
##   <chr>                    <int>       <dbl>      <dbl>
## 1 Genre 1 - hiphop/rap         2         0.2         20
## 2 Genre 4 -  rnb/soul          4         0.4         40
## 3 Genre 5 - alt/indie/folk     2         0.2         20
## 4 Genre 6 - country/rock       2         0.2         20

#This table shows how much each genre comes up in the sample of 10 songs. This data is very different from the percentages I found above. I am going to do 3 more trials with different sample numbers to see if anything changes.

sample_data<-sample_n(playlist_data, 15)
sample_data<-count(sample_data, genre)
head(sample_data)
## # A tibble: 6 × 2
##   genre                        n
##   <chr>                    <int>
## 1 Genre 1 - hiphop/rap         1
## 2 Genre 2 - pop/kpop/Latin     3
## 3 Genre 3 -  house/ska         1
## 4 Genre 4 -  rnb/soul          7
## 5 Genre 5 - alt/indie/folk     2
## 6 Genre 6 - country/rock       1
sample_data<-mutate(sample_data, probability = n/15)
sample_data
## # A tibble: 6 × 3
##   genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         1      0.0667
## 2 Genre 2 - pop/kpop/Latin     3      0.2   
## 3 Genre 3 -  house/ska         1      0.0667
## 4 Genre 4 -  rnb/soul          7      0.467 
## 5 Genre 5 - alt/indie/folk     2      0.133 
## 6 Genre 6 - country/rock       1      0.0667
sample_data<-mutate(sample_data, percentage = probability*100)
sample_data
## # A tibble: 6 × 4
##   genre                        n probability percentage
##   <chr>                    <int>       <dbl>      <dbl>
## 1 Genre 1 - hiphop/rap         1      0.0667       6.67
## 2 Genre 2 - pop/kpop/Latin     3      0.2         20   
## 3 Genre 3 -  house/ska         1      0.0667       6.67
## 4 Genre 4 -  rnb/soul          7      0.467       46.7 
## 5 Genre 5 - alt/indie/folk     2      0.133       13.3 
## 6 Genre 6 - country/rock       1      0.0667       6.67

#In this sample I decided to use 15 songs and looking at the main data and the sample of 10 songs I can see that it is pretty accurate. The numbers may not be the same but they aren’t as far off as I pressumed they would be.

sample_data<-sample_n(playlist_data, 5)
sample_data<-count(sample_data, genre)
head(sample_data)
## # A tibble: 3 × 2
##   genre                        n
##   <chr>                    <int>
## 1 Genre 1 - hiphop/rap         3
## 2 Genre 2 - pop/kpop/Latin     1
## 3 Genre 5 - alt/indie/folk     1
sample_data<-mutate(sample_data, probability = n/5)
sample_data
## # A tibble: 3 × 3
##   genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         3         0.6
## 2 Genre 2 - pop/kpop/Latin     1         0.2
## 3 Genre 5 - alt/indie/folk     1         0.2
sample_data<-mutate(sample_data, percentage = probability*100)
sample_data
## # A tibble: 3 × 4
##   genre                        n probability percentage
##   <chr>                    <int>       <dbl>      <dbl>
## 1 Genre 1 - hiphop/rap         3         0.6         60
## 2 Genre 2 - pop/kpop/Latin     1         0.2         20
## 3 Genre 5 - alt/indie/folk     1         0.2         20

#This sample was of 5 songs making it hard to get a big amount of data. It is not very accurate but if having more songs it would make it way more precise.

sample_data<-sample_n(playlist_data, 30)
sample_data<-count(sample_data, genre)
head(sample_data)
## # A tibble: 6 × 2
##   genre                        n
##   <chr>                    <int>
## 1 Genre 1 - hiphop/rap         6
## 2 Genre 2 - pop/kpop/Latin     5
## 3 Genre 3 -  house/ska         1
## 4 Genre 4 -  rnb/soul          6
## 5 Genre 5 - alt/indie/folk     8
## 6 Genre 6 - country/rock       4
sample_data<-mutate(sample_data, probability = n/30)
sample_data
## # A tibble: 6 × 3
##   genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         6      0.2   
## 2 Genre 2 - pop/kpop/Latin     5      0.167 
## 3 Genre 3 -  house/ska         1      0.0333
## 4 Genre 4 -  rnb/soul          6      0.2   
## 5 Genre 5 - alt/indie/folk     8      0.267 
## 6 Genre 6 - country/rock       4      0.133
sample_data<-mutate(sample_data, percentage = probability*100)
sample_data
## # A tibble: 6 × 4
##   genre                        n probability percentage
##   <chr>                    <int>       <dbl>      <dbl>
## 1 Genre 1 - hiphop/rap         6      0.2         20   
## 2 Genre 2 - pop/kpop/Latin     5      0.167       16.7 
## 3 Genre 3 -  house/ska         1      0.0333       3.33
## 4 Genre 4 -  rnb/soul          6      0.2         20   
## 5 Genre 5 - alt/indie/folk     8      0.267       26.7 
## 6 Genre 6 - country/rock       4      0.133       13.3

#This sample is for 30 songs. This being the most data has the most accurate answers beacause the more data the more precise.

#For my songs I had 3 in Genre 5, 1 in Genre 1 and 1 in Genre 2. Those catagories are shown a lot and have a pretty high percentage compared to other catagories like Genre 3, Genre 4 and Genre 6. This is because the genres my songs were in are more popular than the other genres.

#My theoretical probability and my experimental probability are pretty similar but are almost always going to be different because of the experimental probaility data. The only time they will be the same is if the experimental data uses 144 songs. This shows that the bigger the sample number the more the theoretical probability and the experimental probibility will have in common.

#The Law of Large Numbers is when you do many trials, the experimental probability will approach the true probability. This means for the song experiment that the more trials I do the more accurate the results will be.

#If I were to make a graph with only a few songs it would be very irregular compared to many songs which would make the graph way more dependable.

#My question at the begining was “What is the probability of a specific genre getting played?” The answer is that the probability for Genre 1 is 18%, Genre 2 is 15.3%, Genre 3 is 3.5%, Genre 4 is 29.9%, Genre 5 is 23.6% & Genre 6 is 9.7%. I found this by using the count code as well as the mutate code.

#I think using the coding simulation was very hard but a cool thing to use and learn how to adapt for other projects. I think the most challenging piece of code was the mutate code because I struggled with remembering to save the data frame which was key for that code. I also think the code I am most proud of is also the mutate code because once I figured out how to do it and it clicked it made so much sense and I was very happy and excited.

#I would like to learn more about how to incorporate the data I found and how to turn that into a graph to see it visually. If we were to do something like this again I would want to be less disoriented because I kept jumping from different handouts and confusing myself and I just need to go step by step.