I am trying to make a decision tree using Taylor Swift albums and R isn’t plotting my code so I’m unsure where I went wrong. If anyone could let me know that would be great.
(This R environment comes with many helpful analytics packages installed.)
library(tidyverse) # metapackage of all tidyverse packages
library(reshape2)
library(corrr)
library(readr)
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
library(rpart)
library(rpart.plot)
setwd("C:/Users/kcata/OneDrive - University of Limerick/Desktop/College/College Year 2/Semester 2/Applied Data Analysis MS4034/Project 2")
list.files()
ts_data <- read.csv("taylor_swift_spotify.csv")
ts_tree <- read.csv("taylor_swift_spotify.csv")
ts_data <- ts_data %>% select('name', 'album', 'release_date', 'track_number', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'valence', 'popularity', 'duration_ms', 'music_type' , 'country_preference' , 'pop_preference' , 'indie_preference' , 'mood_preference' , 'upbeat_preference' , 'deluxe_preference')
ts_tree <- ts_tree %>% select('name', 'album', 'release_date', 'track_number', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'valence', 'popularity', 'duration_ms', 'music_type' , 'country_preference' , 'pop_preference' , 'indie_preference' , 'mood_preference' , 'upbeat_preference' , 'deluxe_preference')
dim(unique(ts_data['album']))
ts_data <- ts_data %>% mutate(new_album = case_when(str_detect(album, "Speak Now") ~ "Speak Now",
str_detect(album, "Midnights") ~ "Midnights",
str_detect(album, "Red") ~ "Red",
str_detect(album, "THE TORTURED POETS DEPARTMENT") ~ "TTPD",
str_detect(album, "THE TORTURED POETS DEPARTMENT: THE ANTHOLOGY") ~ "TTPD: THE ANTHOLOGY",
str_detect(album, "Fearless") ~ "Fearless",
str_detect(album, "evermore") ~ "evermore",
str_detect(album, "folklore") ~ "folklore",
str_detect(album, "Lover") ~ "Lover",
str_detect(album, "reputation") ~ "reputation",
str_detect(album, "1989") ~ "1989",
str_detect(album, "Taylor Swift") ~ "Taylor Swift",
TRUE ~ 'Others'
))
`
Function to recommend a Taylor Swift album
recommend_album <- function(music_type, country_preference = NULL, pop_preference = NULL, mood_preference = NULL, indie_preference = NULL, deluxe_preference = NULL) {
# Step 1: Determine the type of music
if (music_type == "Country") {
# Step 2: Country preference
if (country_preference == "Classic Country") {
if (deluxe_preference == TRUE) {
return("Taylor Swift (Deluxe)")
} else {
return("Taylor Swift")
}
} else if (country_preference == "Blend of Country and Pop") {
# Step 3: Ballads or Upbeat
if (mood_preference == "Ballads") {
if (deluxe_preference == TRUE) {
return("Fearless (Platinum Edition)")
} else {
return("Fearless")
}
} else if (mood_preference == "Upbeat") {
# Step 4: Love Songs or Mix
if (upbeat_preference == "Love Songs") {
if (deluxe_preference == TRUE) {
return("Speak Now (Deluxe)")
} else {
return("Speak Now")
}
} else if (upbeat_preference == "Mix of Love and Heartbreak") {
if (deluxe_preference == TRUE) {
return("Red (Deluxe)")
} else {
return("Red")
}
}
}
}
} else if (music_type == "Pop") {
# Step 5:Pop preference
if (pop_preference == "Mainstream Pop") {
# Step 6: Bright/Upbeat or Reflective
if (mood_preference == "Bright/Upbeat") {
if (deluxe_preference == TRUE) {
return("1989 (Deluxe)")
} else {
return("1989")
}
} else if (mood_preference == "Reflective") {
return("Lover")
}
} else if (pop_preference == "Experimental Pop") {
return("Reputation")
}else if (pop_preference == "Dark Pop") {
if (mood_preference == "Midnight Mood") {
if (deluxe_preference == TRUE) {
return("Midnights (3am Edition)")
} else if (deluxe_preference == "YES") {
return("Midnights (The Til Dawn Edition)")
} else {
return("Midnights")
}
}
}
} else if (music_type == "Indie/Folk") {
# Step 7: Indie/Folk preference
if (indie_preference == "Storytelling") {
if (deluxe_preference == TRUE) {
return("Folklore (Deluxe)")
} else {
return("Folklore")
}
} else if (indie_preference == "Moody/Atmospheric") {
if (deluxe_preference == TRUE) {
return("Evermore (Deluxe)")
} else {
return("Evermore")
}
}
} else if (music_type == "Tortured Poets Department") {
if (deluxe_preference == TRUE) {
return("THE TORTURED POETS DEPARTMENT: THE ANTHOLOGY")
} else {
return("THE TORTURED POETS DEPARTMENT")
}
}
# Step 8: From the Vault
if (country_preference == "Fearless Vault") {
return("Fearless (Taylor's Version)")
} else if (country_preference == "Red Vault") {
return("Red (Taylor's Version)")
} else if (country_preference == "Speak Now Vault") {
return("Speak Now (Taylor's Version)")
} else if (pop_preference == "1989 TV") {
if (deluxe_preference == TRUE) {
return("1989 (Taylor's Version) [Deluxe]")
} else {
return("1989 (Taylor's Version)")
}
}
}
# Step 9:Running Decision Tree
Myresult <- rpart(album~., method = "class", data = ts_tree)
rpart.plot(Myresult, type = 3, fallen.leaves = F, cex)
I tried to run this code. I also tried making a data set called ts_tree
to get rid of unnecessary data manipulations. Nothing worked. I could really use a hand.
New contributor
Katelynn Cremin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2