Getting the data
##Get the data from UCI and save it as CSV to post it in GitHub
theURL <- "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
mushrooms <- read.table(theURL, header = FALSE, sep = ",", stringsAsFactors = FALSE)
write.table(mushrooms, "mushroom.csv", sep = ",", row.names = FALSE)
##Use RawGithub to retreive table
theURL <- "https://raw.githubusercontent.com/wheremagichappens/an.dy/master/DATA607/mushroom.csv"
mushrooms <- read.table(theURL, header = TRUE, sep= ",", stringsAsFactors = FALSE)
names(mushrooms) <-c("class","cap-shape","cap-surface","cap-color","bruises","odor","gill-attachment","gill-spacing","gill-size","gill-color","stalk-shape","stalk-root","stalk-surface-above-ring","stalk-surface-below-ring","stalk-color-above-ring","stalk-color-below-ring","veil-type","veil-color","ring-number","ring-type","spore-print-color","population","habitat")
head(mushrooms)
## class cap-shape cap-surface cap-color bruises odor gill-attachment
## 1 p x s n t p f
## 2 e x s y t a f
## 3 e b s w t l f
## 4 p x y w t p f
## 5 e x s g f n f
## 6 e x y y t a f
## gill-spacing gill-size gill-color stalk-shape stalk-root
## 1 c n k e e
## 2 c b k e c
## 3 c b n e c
## 4 c n n e e
## 5 w b k t e
## 6 c b n e c
## stalk-surface-above-ring stalk-surface-below-ring stalk-color-above-ring
## 1 s s w
## 2 s s w
## 3 s s w
## 4 s s w
## 5 s s w
## 6 s s w
## stalk-color-below-ring veil-type veil-color ring-number ring-type
## 1 w p w o p
## 2 w p w o p
## 3 w p w o p
## 4 w p w o p
## 5 w p w o e
## 6 w p w o p
## spore-print-color population habitat
## 1 k s u
## 2 n n g
## 3 n n m
## 4 k s u
## 5 n a g
## 6 k n g
Subsetting the data
##Subsetting, 150 rows 5 columns
mushrooms <- mushrooms[1:150, c(1:4, 22)]
head(mushrooms)
## class cap-shape cap-surface cap-color population
## 1 p x s n s
## 2 e x s y n
## 3 e b s w n
## 4 p x y w s
## 5 e x s g a
## 6 e x y y n
Data replacement
#function replacing class column data sets
rep_class <- function(x){
switch (x,
'p' = 'poisonous',
'e' = 'edible'
)
}
#function replacing cap-shape column data sets
rep_capshape <- function(x){
switch (x,
'b' = 'bell',
'c' = 'conical',
'x' = 'convex',
'f' = 'flat',
'k' = 'knobbed',
's' = 'sunken'
)
}
#function replacing cap-surface column data sets
rep_capsurface <- function(x){
switch (x,
'f' = 'fibrous',
'g' = 'grooves',
'y' = 'scaly',
's' = 'smooth'
)
}
#function replacing cap-color column data sets
rep_capcolor <- function(x){
switch (x,
'n' = 'brown',
'b' = 'buff',
'c' = 'cinnamon',
'g' = 'gray',
'r' = 'green',
'p' = 'pink',
'u' = 'purple',
'e' = 'red',
'w' = 'white',
'y' = 'yellow'
)
}
#function replacing population column data sets
rep_pop <- function(x){
switch (x,
'a' = 'abundant',
'c' = 'clustered',
'n' = 'numerous',
's' = 'scattered',
'v' = 'several',
'y' = 'solitary'
)
}
Applying data replacement
names(mushrooms)
## [1] "class" "cap-shape" "cap-surface" "cap-color" "population"
mushrooms$class <-sapply(mushrooms$class, rep_class)
mushrooms$`cap-shape` <-sapply(mushrooms$`cap-shape`, rep_capshape)
mushrooms$`cap-surface` <-sapply(mushrooms$`cap-surface`, rep_capsurface)
mushrooms$`cap-color` <-sapply(mushrooms$`cap-color`, rep_capcolor)
mushrooms$population <-sapply(mushrooms$population, rep_pop)
mushrooms
## class cap-shape cap-surface cap-color population
## 1 poisonous convex smooth brown scattered
## 2 edible convex smooth yellow numerous
## 3 edible bell smooth white numerous
## 4 poisonous convex scaly white scattered
## 5 edible convex smooth gray abundant
## 6 edible convex scaly yellow numerous
## 7 edible bell smooth white numerous
## 8 edible bell scaly white scattered
## 9 poisonous convex scaly white several
## 10 edible bell smooth yellow scattered
## 11 edible convex scaly yellow numerous
## 12 edible convex scaly yellow scattered
## 13 edible bell smooth yellow scattered
## 14 poisonous convex scaly white several
## 15 edible convex fibrous brown abundant
## 16 edible sunken fibrous gray solitary
## 17 edible flat fibrous white abundant
## 18 poisonous convex smooth brown scattered
## 19 poisonous convex scaly white scattered
## 20 poisonous convex smooth brown scattered
## 21 edible bell smooth yellow scattered
## 22 poisonous convex scaly brown several
## 23 edible bell scaly yellow scattered
## 24 edible bell scaly white numerous
## 25 edible bell smooth white scattered
## 26 poisonous flat smooth white several
## 27 edible convex scaly yellow numerous
## 28 edible convex scaly white numerous
## 29 edible flat fibrous brown solitary
## 30 edible convex smooth yellow several
## 31 edible bell smooth yellow numerous
## 32 poisonous convex scaly white scattered
## 33 edible convex scaly yellow numerous
## 34 edible convex scaly brown solitary
## 35 edible bell scaly yellow scattered
## 36 edible convex fibrous yellow several
## 37 edible sunken fibrous gray several
## 38 poisonous convex scaly brown scattered
## 39 edible convex fibrous yellow several
## 40 edible bell smooth yellow scattered
## 41 edible bell scaly yellow scattered
## 42 edible convex scaly yellow solitary
## 43 edible convex fibrous brown solitary
## 44 poisonous convex scaly white several
## 45 edible convex smooth yellow numerous
## 46 edible convex scaly white numerous
## 47 edible convex scaly yellow scattered
## 48 edible convex smooth white numerous
## 49 edible convex scaly yellow scattered
## 50 edible flat scaly yellow scattered
## 51 edible convex scaly brown scattered
## 52 edible convex smooth white scattered
## 53 edible bell smooth white numerous
## 54 poisonous convex scaly brown several
## 55 poisonous convex smooth white several
## 56 edible bell scaly yellow scattered
## 57 edible flat fibrous gray abundant
## 58 edible bell smooth white numerous
## 59 edible convex smooth yellow numerous
## 60 edible convex scaly brown solitary
## 61 edible sunken fibrous gray several
## 62 edible bell scaly yellow scattered
## 63 edible bell smooth yellow scattered
## 64 edible bell scaly yellow numerous
## 65 edible bell scaly white scattered
## 66 edible flat smooth brown abundant
## 67 edible convex smooth white scattered
## 68 edible flat scaly yellow scattered
## 69 edible convex scaly yellow numerous
## 70 edible convex fibrous gray several
## 71 edible flat fibrous yellow several
## 72 edible bell scaly white scattered
## 73 edible flat fibrous yellow several
## 74 edible convex scaly brown scattered
## 75 edible bell smooth yellow scattered
## 76 edible flat smooth yellow several
## 77 edible convex smooth white several
## 78 edible flat scaly brown solitary
## 79 poisonous convex scaly brown several
## 80 edible flat scaly brown solitary
## 81 edible convex smooth brown scattered
## 82 poisonous convex scaly white scattered
## 83 edible flat fibrous gray solitary
## 84 edible convex fibrous gray scattered
## 85 edible convex scaly yellow scattered
## 86 edible convex smooth brown scattered
## 87 edible bell smooth white scattered
## 88 edible convex smooth white scattered
## 89 edible flat scaly brown solitary
## 90 edible sunken fibrous brown several
## 91 edible convex fibrous brown solitary
## 92 edible bell smooth white scattered
## 93 edible convex scaly yellow scattered
## 94 edible convex scaly yellow numerous
## 95 edible convex smooth brown abundant
## 96 edible convex smooth white scattered
## 97 edible flat scaly brown scattered
## 98 edible convex smooth yellow numerous
## 99 edible bell smooth white scattered
## 100 edible convex scaly white scattered
## 101 edible convex fibrous brown scattered
## 102 edible bell smooth yellow numerous
## 103 edible flat scaly yellow scattered
## 104 edible convex scaly yellow solitary
## 105 edible bell scaly white numerous
## 106 edible convex scaly yellow numerous
## 107 edible convex scaly yellow solitary
## 108 edible bell scaly white scattered
## 109 edible bell scaly white scattered
## 110 edible convex smooth yellow numerous
## 111 edible convex smooth yellow numerous
## 112 edible sunken fibrous gray solitary
## 113 edible convex fibrous white several
## 114 edible convex smooth yellow numerous
## 115 poisonous convex scaly white several
## 116 edible convex scaly yellow scattered
## 117 edible sunken fibrous gray solitary
## 118 edible convex scaly yellow solitary
## 119 edible convex smooth yellow several
## 120 edible sunken fibrous brown solitary
## 121 poisonous convex smooth white several
## 122 edible convex scaly white numerous
## 123 poisonous flat scaly brown several
## 124 edible flat smooth gray abundant
## 125 edible convex smooth yellow scattered
## 126 edible convex smooth white scattered
## 127 edible bell smooth yellow numerous
## 128 edible flat fibrous gray abundant
## 129 edible convex smooth white numerous
## 130 edible bell smooth white scattered
## 131 edible bell smooth white scattered
## 132 edible bell scaly white scattered
## 133 edible flat smooth white several
## 134 edible convex scaly yellow scattered
## 135 edible flat smooth white several
## 136 poisonous convex scaly white several
## 137 edible flat fibrous white several
## 138 edible convex scaly yellow scattered
## 139 poisonous convex smooth brown several
## 140 edible bell smooth yellow numerous
## 141 edible convex scaly brown solitary
## 142 edible bell scaly yellow numerous
## 143 edible sunken fibrous brown several
## 144 edible flat scaly brown solitary
## 145 edible convex scaly yellow numerous
## 146 edible convex fibrous gray scattered
## 147 edible flat fibrous white abundant
## 148 edible convex scaly yellow numerous
## 149 edible bell smooth yellow numerous
## 150 edible bell scaly white numerous