#Import the Mushroom Data

fileUrl <- "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
mushrooms_data <- read.table(file = fileUrl, header = FALSE, sep = ",")
head(mushrooms_data)
##   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
## 1  p  x  s  n  t  p  f  c  n   k   e   e   s   s   w   w   p   w   o   p
## 2  e  x  s  y  t  a  f  c  b   k   e   c   s   s   w   w   p   w   o   p
## 3  e  b  s  w  t  l  f  c  b   n   e   c   s   s   w   w   p   w   o   p
## 4  p  x  y  w  t  p  f  c  n   n   e   e   s   s   w   w   p   w   o   p
## 5  e  x  s  g  f  n  f  w  b   k   t   e   s   s   w   w   p   w   o   e
## 6  e  x  y  y  t  a  f  c  b   n   e   c   s   s   w   w   p   w   o   p
##   V21 V22 V23
## 1   k   s   u
## 2   n   n   g
## 3   n   n   m
## 4   k   s   u
## 5   n   a   g
## 6   k   n   g
#Columns: Edible/Poisonous, Cap-Shape, Cap-Surface, Cap-Color, and Odor.

#create data frame with only the required columns

mushrooms1 <- mushrooms_data[,1:4]
mushrooms2 <- mushrooms_data[,6]
#The two Dataframes are combined into one data frame
mushrooms <- cbind(mushrooms1, mushrooms2)
head(mushrooms)
##   V1 V2 V3 V4 mushrooms2
## 1  p  x  s  n          p
## 2  e  x  s  y          a
## 3  e  b  s  w          l
## 4  p  x  y  w          p
## 5  e  x  s  g          n
## 6  e  x  y  y          a
#Assign Column Names
colnames(mushrooms) <- c("Edible/Poisonous","Cap-Shape","Cap-Surface","Cap-Color","Odor")
head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color Odor
## 1                p         x           s         n    p
## 2                e         x           s         y    a
## 3                e         b           s         w    l
## 4                p         x           y         w    p
## 5                e         x           s         g    n
## 6                e         x           y         y    a
#Edible/Poisonous
#Add the levels for every Edible/Poisonous
levels(mushrooms$`Edible/Poisonous`) <- c(levels(mushrooms$`Edible/Poisonous`), c("Edible","Poisonous"))

mushrooms$`Edible/Poisonous`[mushrooms$`Edible/Poisonous` == "e"] <- "Edible"
mushrooms$`Edible/Poisonous`[mushrooms$`Edible/Poisonous` == "p"] <- "Poisonous"

head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color Odor
## 1        Poisonous         x           s         n    p
## 2           Edible         x           s         y    a
## 3           Edible         b           s         w    l
## 4        Poisonous         x           y         w    p
## 5           Edible         x           s         g    n
## 6           Edible         x           y         y    a
#Add the levels for every Cap-Shape

levels(mushrooms$`Cap-Shape`) <- c(levels(mushrooms$`Cap-Shape`), c("Bell","Conical","Convex","Flat","Knobbed","Sunken"))
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "b"] <- "Bell"
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "c"] <- "Conical"
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "x"] <- "Convex"
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "f"] <- "Flat"
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "k"] <- "Knobbed"
mushrooms$`Cap-Shape`[mushrooms$`Cap-Shape` == "s"] <- "Sunken"

head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color Odor
## 1        Poisonous    Convex           s         n    p
## 2           Edible    Convex           s         y    a
## 3           Edible      Bell           s         w    l
## 4        Poisonous    Convex           y         w    p
## 5           Edible    Convex           s         g    n
## 6           Edible    Convex           y         y    a
##Add the levels for every Cap-Surface
levels(mushrooms$`Cap-Surface`) <- c(levels(mushrooms$`Cap-Surface`), c("Fibrous", "Grooves", "Scaly", "Smooth"))
mushrooms$`Cap-Surface`[mushrooms$`Cap-Surface` == "f"] <- "Fibrous"
mushrooms$`Cap-Surface`[mushrooms$`Cap-Surface` == "g"] <- "Grooves"
mushrooms$`Cap-Surface`[mushrooms$`Cap-Surface` == "y"] <- "Scaly"
mushrooms$`Cap-Surface`[mushrooms$`Cap-Surface` == "s"] <- "Smooth"
head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color Odor
## 1        Poisonous    Convex      Smooth         n    p
## 2           Edible    Convex      Smooth         y    a
## 3           Edible      Bell      Smooth         w    l
## 4        Poisonous    Convex       Scaly         w    p
## 5           Edible    Convex      Smooth         g    n
## 6           Edible    Convex       Scaly         y    a
##Add the levels for every Cap-Color
levels(mushrooms$`Cap-Color`) <- c(levels(mushrooms$`Cap-Color`), c("Brown", "Buff", "Cinnamon", "Gray", "Green", "Pink", "Purple", "Red", "White", "Yellow"))
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "n"] <- "Brown"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "b"] <- "Buff"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "c"] <- "Cinnamon"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "g"] <- "Gray"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "r"] <- "Green"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "p"] <- "Pink"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "u"] <- "Purple"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "e"] <- "Red"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "w"] <- "White"
mushrooms$`Cap-Color`[mushrooms$`Cap-Color` == "y"] <- "Yellow"
head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color Odor
## 1        Poisonous    Convex      Smooth     Brown    p
## 2           Edible    Convex      Smooth    Yellow    a
## 3           Edible      Bell      Smooth     White    l
## 4        Poisonous    Convex       Scaly     White    p
## 5           Edible    Convex      Smooth      Gray    n
## 6           Edible    Convex       Scaly    Yellow    a
##Add the levels for every Odor
levels(mushrooms$Odor) <- c(levels(mushrooms$Odor), c("Almond", "Anise", "Creosote", "Fishy", "Foul", "Musty", "None", "Pungent", "Spicy"))
mushrooms$Odor[mushrooms$Odor == "a"] <- "Almond"
mushrooms$Odor[mushrooms$Odor == "l"] <- "Anise"
mushrooms$Odor[mushrooms$Odor == "c"] <- "Creosote"
mushrooms$Odor[mushrooms$Odor == "y"] <- "Fishy"
mushrooms$Odor[mushrooms$Odor == "f"] <- "Foul"
mushrooms$Odor[mushrooms$Odor == "m"] <- "Musty"
mushrooms$Odor[mushrooms$Odor == "n"] <- "None"
mushrooms$Odor[mushrooms$Odor == "p"] <- "Pungent"
mushrooms$Odor[mushrooms$Odor == "s"] <- "Spicy"


#Final Output
head(mushrooms)
##   Edible/Poisonous Cap-Shape Cap-Surface Cap-Color    Odor
## 1        Poisonous    Convex      Smooth     Brown Pungent
## 2           Edible    Convex      Smooth    Yellow  Almond
## 3           Edible      Bell      Smooth     White   Anise
## 4        Poisonous    Convex       Scaly     White Pungent
## 5           Edible    Convex      Smooth      Gray    None
## 6           Edible    Convex       Scaly    Yellow  Almond