Open data file and selected target columns
rawdata <- read.table("agaricus-lepiota.data", header=FALSE, sep=",")
subdata <- subset(rawdata, select =c(V1,V9,V11,V17,V18))
Create 5 functions to parse the contents of “rawdata”
edible = function(x)
{
if (x == 'e')
return("Edible")
if (x=='p')
return("Poisonos")
}
gillsize = function(x)
{
if (x=='b')
return('Broad')
if (x=='n')
return('Narrow')
}
starkshape = function(x)
{
if (x=='e')
return('Enlargin')
if (x=='t')
return('Tapering')
}
veiltype = function(x)
{
if (x=='p')
return('Partial')
if (x=='u')
return('Universal')
}
veilcolor = function(x)
{
if (x=='n')
return('Brown')
if (x=='o')
return('Orange')
if (x=='w')
return('White')
if (x=='y')
return('Yellow')
}
Testing the custom functions
edible('e')
## [1] "Edible"
edible('p')
## [1] "Poisonos"
gillsize('n')
## [1] "Narrow"
gillsize('b')
## [1] "Broad"
starkshape('e')
## [1] "Enlargin"
starkshape('t')
## [1] "Tapering"
veiltype('p')
## [1] "Partial"
veiltype('u')
## [1] "Universal"
veilcolor('o')
## [1] "Orange"
veilcolor('w')
## [1] "White"
veilcolor('y')
## [1] "Yellow"
Apply creating functions to each of the columns
col1 = unlist(lapply(subdata[,1], edible))
col2 = unlist(lapply(subdata[,2], gillsize))
col3 = unlist(lapply(subdata[,3], starkshape))
col4 = unlist(lapply(subdata[,4], veiltype))
col5 = unlist(lapply(subdata[,5], veilcolor))
Bind columns into one data frame object Rename Column Headers
finaldata = cbind(col1, col2, col3, col4, col5)
colnames(finaldata) <-c("Edible/Poisonous", "Gill Size", "Stalk Shape", "Veil Type", "Veil Color")
Display top lines of final dataframe object
head(finaldata)
## Edible/Poisonous Gill Size Stalk Shape Veil Type Veil Color
## [1,] "Poisonos" "Narrow" "Enlargin" "Partial" "White"
## [2,] "Edible" "Broad" "Enlargin" "Partial" "White"
## [3,] "Edible" "Broad" "Enlargin" "Partial" "White"
## [4,] "Poisonos" "Narrow" "Enlargin" "Partial" "White"
## [5,] "Edible" "Broad" "Tapering" "Partial" "White"
## [6,] "Edible" "Broad" "Enlargin" "Partial" "White"