How to plot a decision tree?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật