This is an R Markdown document pertaining to week 03 assignment in R. This assignment demonstrate: 1. fetching data web site and creation of data frame 2. subsetting the data frame (columns and rows) 3. renaming of the columns 4. replacing values in data frame
Note: the code is done in base R
# import data set from web page into data frame
dfmushrooms=read.csv(file="https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data", header=FALSE, skip=0)
# display characteristics of dataframe and column names
str(dfmushrooms)
## 'data.frame': 8124 obs. of 23 variables:
## $ V1 : Factor w/ 2 levels "e","p": 2 1 1 2 1 1 1 1 2 1 ...
## $ V2 : Factor w/ 6 levels "b","c","f","k",..: 6 6 1 6 6 6 1 1 6 1 ...
## $ V3 : Factor w/ 4 levels "f","g","s","y": 3 3 3 4 3 4 3 4 4 3 ...
## $ V4 : Factor w/ 10 levels "b","c","e","g",..: 5 10 9 9 4 10 9 9 9 10 ...
## $ V5 : Factor w/ 2 levels "f","t": 2 2 2 2 1 2 2 2 2 2 ...
## $ V6 : Factor w/ 9 levels "a","c","f","l",..: 7 1 4 7 6 1 1 4 7 1 ...
## $ V7 : Factor w/ 2 levels "a","f": 2 2 2 2 2 2 2 2 2 2 ...
## $ V8 : Factor w/ 2 levels "c","w": 1 1 1 1 2 1 1 1 1 1 ...
## $ V9 : Factor w/ 2 levels "b","n": 2 1 1 2 1 1 1 1 2 1 ...
## $ V10: Factor w/ 12 levels "b","e","g","h",..: 5 5 6 6 5 6 3 6 8 3 ...
## $ V11: Factor w/ 2 levels "e","t": 1 1 1 1 2 1 1 1 1 1 ...
## $ V12: Factor w/ 5 levels "?","b","c","e",..: 4 3 3 4 4 3 3 3 4 3 ...
## $ V13: Factor w/ 4 levels "f","k","s","y": 3 3 3 3 3 3 3 3 3 3 ...
## $ V14: Factor w/ 4 levels "f","k","s","y": 3 3 3 3 3 3 3 3 3 3 ...
## $ V15: Factor w/ 9 levels "b","c","e","g",..: 8 8 8 8 8 8 8 8 8 8 ...
## $ V16: Factor w/ 9 levels "b","c","e","g",..: 8 8 8 8 8 8 8 8 8 8 ...
## $ V17: Factor w/ 1 level "p": 1 1 1 1 1 1 1 1 1 1 ...
## $ V18: Factor w/ 4 levels "n","o","w","y": 3 3 3 3 3 3 3 3 3 3 ...
## $ V19: Factor w/ 3 levels "n","o","t": 2 2 2 2 2 2 2 2 2 2 ...
## $ V20: Factor w/ 5 levels "e","f","l","n",..: 5 5 5 5 1 5 5 5 5 5 ...
## $ V21: Factor w/ 9 levels "b","h","k","n",..: 3 4 4 3 4 3 3 4 3 3 ...
## $ V22: Factor w/ 6 levels "a","c","n","s",..: 4 3 3 4 1 3 3 4 5 4 ...
## $ V23: Factor w/ 7 levels "d","g","l","m",..: 6 2 4 6 2 2 4 4 2 4 ...
names(dfmushrooms)
## [1] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11"
## [12] "V12" "V13" "V14" "V15" "V16" "V17" "V18" "V19" "V20" "V21" "V22"
## [23] "V23"
# subset columns 1, 2, 3, 4, & 5 from original data frame
dfmushrooms_c <- dfmushrooms[, c(1, 2, 3, 4, 6)]
# subset rows 1 - 50
dfmushrooms_c_r <- dfmushrooms_c[1:50,]
# rename columns of reduced data frame
names(dfmushrooms_c_r) <- c("Edibility", "Caps Shape", "Caps Surface", "Caps Color", "Odor")
# display column name
names(dfmushrooms_c_r)
## [1] "Edibility" "Caps Shape" "Caps Surface" "Caps Color"
## [5] "Odor"
# remap value for each column for more significant entries
# Edibility column, col1
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Edibility)[levels(Edibility) == "p"] <- "poisoneous")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Edibility)[levels(Edibility) == "e"] <- "edible")
# Caps Shape column, col2
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "b"] <- "bell")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "c"] <- "conical")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "x"] <- "convex")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "f"] <- "flat")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "k"] <- "knobbed")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Shape`)[levels(`Caps Shape`) == "s"] <- "sunken")
# Caps Surface column, col3
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Surface`)[levels(`Caps Surface`) == "f"] <- "fibrous")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Surface`)[levels(`Caps Surface`) == "g"] <- "grooves")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Surface`)[levels(`Caps Surface`) == "y"] <- "scaly")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Surface`)[levels(`Caps Surface`) == "s"] <- "smooth")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Surface`)[levels(`Caps Surface`) == "f"] <- "fibrous")
# Caps Color column, col4
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "n"] <- "brown")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "b"] <- "buff")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "c"] <- "cinamon")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "g"] <- "gray")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "r"] <- "green")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "p"] <- "pink")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "u"] <- "purple")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "e"] <- "red")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "w"] <- "white")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(`Caps Color`)[levels(`Caps Color`) == "y"] <- "yellow")
# Odor, col5 (6 from original dataframe)
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "a"] <- "almond")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "l"] <- "anise")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "c"] <- "creosote")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "y"] <- "fishy")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "f"] <- "foul")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "m"] <- "musty")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "n"] <- "none")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "p"] <- "pungent")
dfmushrooms_c_r <-within(dfmushrooms_c_r, levels(Odor)[levels(Odor) == "s"] <- "spicy")
# display transformed data frame
dfmushrooms_c_r
## Edibility Caps Shape Caps Surface Caps Color Odor
## 1 poisoneous convex smooth brown pungent
## 2 edible convex smooth yellow almond
## 3 edible bell smooth white anise
## 4 poisoneous convex scaly white pungent
## 5 edible convex smooth gray none
## 6 edible convex scaly yellow almond
## 7 edible bell smooth white almond
## 8 edible bell scaly white anise
## 9 poisoneous convex scaly white pungent
## 10 edible bell smooth yellow almond
## 11 edible convex scaly yellow anise
## 12 edible convex scaly yellow almond
## 13 edible bell smooth yellow almond
## 14 poisoneous convex scaly white pungent
## 15 edible convex fibrous brown none
## 16 edible sunken fibrous gray none
## 17 edible flat fibrous white none
## 18 poisoneous convex smooth brown pungent
## 19 poisoneous convex scaly white pungent
## 20 poisoneous convex smooth brown pungent
## 21 edible bell smooth yellow almond
## 22 poisoneous convex scaly brown pungent
## 23 edible bell scaly yellow anise
## 24 edible bell scaly white almond
## 25 edible bell smooth white anise
## 26 poisoneous flat smooth white pungent
## 27 edible convex scaly yellow almond
## 28 edible convex scaly white anise
## 29 edible flat fibrous brown none
## 30 edible convex smooth yellow almond
## 31 edible bell smooth yellow anise
## 32 poisoneous convex scaly white pungent
## 33 edible convex scaly yellow anise
## 34 edible convex scaly brown anise
## 35 edible bell scaly yellow anise
## 36 edible convex fibrous yellow anise
## 37 edible sunken fibrous gray none
## 38 poisoneous convex scaly brown pungent
## 39 edible convex fibrous yellow almond
## 40 edible bell smooth yellow anise
## 41 edible bell scaly yellow almond
## 42 edible convex scaly yellow anise
## 43 edible convex fibrous brown none
## 44 poisoneous convex scaly white pungent
## 45 edible convex smooth yellow almond
## 46 edible convex scaly white almond
## 47 edible convex scaly yellow anise
## 48 edible convex smooth white anise
## 49 edible convex scaly yellow anise
## 50 edible flat scaly yellow anise