5N9 L.VIVEK 2023-10-17 R Markdown This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

data=read.csv(“C:/Users/java/Documents/spotify.csv”) summary(data) ## track_name artist.s._name artist_count released_year ## Length:911 Length:911 Min. :1.000 Min. :1975
## Class :character Class :character 1st Qu.:1.000 1st Qu.:2022
## Mode :character Mode :character Median :1.000 Median :2023
## Mean :1.566 Mean :2021
## 3rd Qu.:2.000 3rd Qu.:2023
## Max. :8.000 Max. :2023
## NA’s :812 NA’s :812
## released_month released_day in_spotify_playlists in_spotify_charts ## Min. : 1.000 Min. : 1.00 Min. : 31 Min. : 9.00
## 1st Qu.: 3.000 1st Qu.: 7.00 1st Qu.: 1050 1st Qu.: 36.00
## Median : 6.000 Median :17.00 Median : 2953 Median : 45.00
## Mean : 5.677 Mean :15.54 Mean : 6324 Mean : 52.64
## 3rd Qu.: 7.000 3rd Qu.:23.00 3rd Qu.: 7890 3rd Qu.: 67.00
## Max. :12.000 Max. :31.00 Max. :43899 Max. :147.00
## NA’s :812 NA’s :812 NA’s :812 NA’s :812
## streams in_apple_playlists in_apple_charts in_deezer_playlists ## Min. :1.478e+07 Min. : 0.0 Min. : 0.0 Length:911
## 1st Qu.:1.192e+08 1st Qu.: 34.0 1st Qu.: 75.5 Class :character
## Median :3.732e+08 Median : 68.0 Median :110.0 Mode :character
## Mean :6.830e+08 Mean :105.9 Mean :112.4
## 3rd Qu.:9.917e+08 3rd Qu.:115.5 3rd Qu.:146.5
## Max. :3.704e+09 Max. :672.0 Max. :263.0
## NA’s :812 NA’s :812 NA’s :812
## in_deezer_charts in_shazam_charts bpm key
## Min. : 0.00 Length:911 Min. : 67.0 Length:911
## 1st Qu.: 1.50 Class :character 1st Qu.:100.5 Class :character
## Median : 8.00 Mode :character Median :128.0 Mode :character
## Mean :10.63 Mean :125.4
## 3rd Qu.:14.00 3rd Qu.:140.0
## Max. :58.00 Max. :204.0
## NA’s :812 NA’s :812
## mode danceability_. valence_. energy_.
## Length:911 Min. :34.00 Min. :10.00 Min. : 9.00
## Class :character 1st Qu.:56.00 1st Qu.:36.00 1st Qu.:58.00
## Mode :character Median :67.00 Median :52.00 Median :68.00
## Mean :66.74 Mean :52.56 Mean :66.29
## 3rd Qu.:77.50 3rd Qu.:70.50 3rd Qu.:76.00
## Max. :92.00 Max. :96.00 Max. :97.00
## NA’s :812 NA’s :812 NA’s :812
## acousticness_. instrumentalness_. liveness_. speechiness_.
## Min. : 0.00 Min. : 0.00 Min. : 3.00 Min. : 3.000
## 1st Qu.: 7.00 1st Qu.: 0.00 1st Qu.:10.00 1st Qu.: 4.000
## Median :17.00 Median : 0.00 Median :11.00 Median : 5.000
## Mean :26.17 Mean : 1.96 Mean :17.29 Mean : 7.293
## 3rd Qu.:43.00 3rd Qu.: 0.00 3rd Qu.:23.00 3rd Qu.: 7.000
## Max. :96.00 Max. :63.00 Max. :83.00 Max. :33.000
## NA’s :812 NA’s :812 NA’s :812 NA’s :812 Including Plots You can also embed plots, for example:

# Explore the mtcars data frame with str() str(data) ## ‘data.frame’: 911 obs. of 24 variables: ## $ track_name : chr “Seven (feat. Latto) (Explicit Ver.)” “LALA” “vampire” “Cruel Summer” … ## $ artist.s._name : chr “Latto, Jung Kook” “Myke Towers” “Olivia Rodrigo” “Taylor Swift” … ## $ artist_count : int 2 1 1 1 1 2 2 1 1 2 … ## $ released_year : int 2023 2023 2023 2019 2023 2023 2023 2023 2023 2023 … ## $ released_month : int 7 3 6 8 5 6 3 7 5 3 … ## $ released_day : int 14 23 30 23 18 1 16 7 15 17 … ## $ in_spotify_playlists: int 553 1474 1397 7858 3133 2186 3090 714 1096 2953 … ## $ in_spotify_charts : int 147 48 113 100 50 91 50 43 83 44 … ## $ streams : num 1.41e+08 1.34e+08 1.40e+08 8.01e+08 3.03e+08 … ## $ in_apple_playlists : int 43 48 94 116 84 67 34 25 60 49 … ## $ in_apple_charts : int 263 126 207 207 133 213 222 89 210 110 … ## $ in_deezer_playlists : chr “45” “58” “91” “125” … ## $ in_deezer_charts : int 10 14 14 12 15 17 13 13 11 13 … ## $ in_shazam_charts : chr “826” “382” “949” “548” … ## $ bpm : int 125 92 138 170 144 141 148 100 130 170 … ## $ key : chr “B” “C#” “F” “A” … ## $ mode : chr “Major” “Major” “Major” “Major” … ## $ danceability_. : int 80 71 51 55 65 92 67 67 85 81 … ## $ valence_. : int 89 61 32 58 23 66 83 26 22 56 … ## $ energy_. : int 83 74 53 72 80 58 76 71 62 48 … ## $ acousticness_. : int 31 7 17 11 14 19 48 37 12 21 … ## $ instrumentalness_. : int 0 0 0 0 63 0 0 0 0 0 … ## $ liveness_. : int 8 10 31 11 11 8 8 11 28 8 … ## $ speechiness_. : int 4 4 6 15 6 24 3 4 9 33 … #It plots the data plot(data)

library(ggplot2) #Data Layer ggplot(data = data) + labs(title =“Spotify songs-2023”)

It gives the aesthetic Layer

ggplot(data = data, aes(x = valence_., y = energy_., col = acousticness_. ))+labs(title = “Spotify songs Data Plot”)

Geometric layer

ggplot(data = data, aes(x = valence_., y = energy_., col = acousticness_.)) + geom_point() + labs(title = “valence_. vs energy_.”, x = “valence_.”, y = “energy_.”) ## Warning: Removed 812 rows containing missing values (geom_point()).

#Histogram plot by specifying size ggplot(data = data, aes(x =valence_. , y = energy_., size =acousticness_.)) + geom_point() + labs(title = “valence_. vs energy_.”, x = “valence_.”, y = ” energy_.”) ## Warning: Removed 812 rows containing missing values (geom_point()).

Adding shape and color

ggplot(data = data, aes(x =valence_. , y = energy_., col = factor(artist_count ), shape = factor(speechiness_.))) +geom_point() + labs(title = “valence_. vs energy_.”, x = “valence_.”, y = “energy_.”) ## Warning: The shape palette can deal with a maximum of 6 discrete values because ## more than 6 becomes difficult to discriminate; you have 19. Consider ## specifying shapes manually if you must have them. ## Warning: Removed 832 rows containing missing values (geom_point()).

A scatter plot

data\(valence_.<-factor(data\)valence_.) ggplot(data, aes(x = factor(valence_.), y =energy_. )) + geom_point() ## Warning: Removed 812 rows containing missing values (geom_point()).

Histogram plot

data\(valence_.=as.integer(data\)valence_.) ggplot(data = data, aes(x =valence_. )) + geom_histogram(binwidth = 5,color=“black”, fill=“lightblue”) + labs(title = “Histogram of valence_.”, x = “valence_.”, y = “Count”) ## Warning: Removed 812 rows containing non-finite values (stat_bin()).

#Boxplot – geom_boxplot() Scatter plot – geom_point()

ggplot(data = data, aes(x=as.factor(bpm), fill=bpm )) + geom_bar(stat=“count”)

valence_..type = table(data\(valence_.) valence_..acousticness_. = table(data\)valence_., data$acousticness_.) barplot(valence_..type, main=“No of valence Frequency”, xlab=“No. of vaence”,ylab=“Frequency of spotify songs”,names.arg=names(valence_..type),col=c(“blue”,“green”,“yellow”),legend = rownames(valence_..acousticness_.))

liveness_. = table(data$liveness_. ) data.labels = names(liveness_. ) share = round(liveness_./sum(liveness_.)*100) data.labels = paste(data.labels, share) data.labels = paste(data.labels,“%”,sep=““) pie(liveness_.,labels = data.labels,clockwise=TRUE, col=heat.colors(length(data.labels)), main=”Frequency of liveness value”)

bx <- ggplot(data = data, aes(x = factor(bpm), y = valence_. )) + geom_boxplot(fill = “blue”) + ggtitle(“Distribution of songs”) + ylab(“valence”) + xlab(“bpm”) bx ## Warning: Removed 812 rows containing non-finite values (stat_boxplot()).

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.