DS4 Song Simulation

Ada Currey

We need to load tidyverse.

{r} install.packages("tidyverse") {r} library(tidyverse)

{r} df <- read_csv("Playlist - Clean.csv") {r} head(df) {r} df$genre <- as.factor(df$Genre) {r} df %>% count(Genre) {r} genre_counts <- df %>% count(Genre) ```{r} genre_counts <- genre_counts %>% mutate(prob = n/sum(n)) genre_counts

```{r}
genre_counts$prob <- round(genre_counts$prob, 2)
genre_counts

Gathering & Organizing Data

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.

{r} # load necessary libraries library(tidyverse) ```{r} # import data df <- read_csv(“Playlist - Clean.csv”)

```{r}
#check first few rows of data
head(df)

```{r} # change variable type to factor df\(Genre <- as.factor(df\)Genre) # get the counts for each genre df %>% count(Genre)

```{r}
Genre_counts <- df %>% count(Genre)

#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.

{r} genre_counts <- genre_counts %>% mutate(prob = n/sum(n)) genre_counts

{r} genre_counts$prob <- round(genre_counts$prob, 2) genre_counts

#Calculating Theoretical Possibilities

```{r} sample <- sample_n(df, 10) sample

```{r}
sample_counts <- sample %>% count(Genre) %>% mutate(probability = n/sum(n))
sample_counts

#Combining Class Values

```{r} combined <- tibble(Genre = Genre_counts$Genre, n = c(2, 3, 0, 1, 2, 2), probability = n/sum(n))

combined

#More Simulations of Shuffle

```{r}
sample_n(df, 20) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))

{r} sample_n(df, 30) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2)) {r} sample_n(df, 50) %>% count(Genre) %>% mutate(probability = n/sum(n)) {r} sample_n(df, 20) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2)) {r} sample_n(df, 30) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2)) {r} sample_n(df, 40) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2)) ```{r} sample_n(df, 175) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))

```{r}
sample_n(df, 10) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))

```{r} sample_n(df, 1) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))

``` # Analyze and Synthesize

The genres you hear on shuffle represent our class playlist more and more the more times you play it. You can see more songs shuffled shows more accurately the class song data. Experimental probability grows closer and closer to theoretical probability the more times you try it.

Reflection

I feel like I had a lot of trouble with coding. I didnt get it and all the info went in one ear and out the other. What I did to make progress was ask a lot of questions and have Preston walk me through everything til I got it. It was frustrating but it felt soooo good once I understood. I think this just teaches me to have more patience and not overthink things. I wonder if I will struggle as much next time we code, I doubt it.