## ----covtype_readdata---------------------------------------------------- covtype <- read.csv(file="Data/CoverageType/covtype.data", header=FALSE, sep=",", stringsAsFactors=FALSE) head(covtype,n = 2) ## ----covtype_name-------------------------------------------------------- # "Fixed" attributes' names names <- c("Elevation","Aspect","Slope","HorDistToHydro","VertDistToHydro", "HorDistRoad","Hillshade09","Hillshade12","Hillshade15", "HorDistFire") # Four binary attributes for wilderness areas: names <- c(names,"WA_RWA","WA_NWA","WA_CPWA","WA_CLPWA") # 40 (!) binary attributes for soil types: names <- c(names,sprintf("ST%02d",1:40)) # The cover type names <- c(names,"Class") # Assign these names to the attributes names(covtype) <- names # Let's also assign labels to the coverage types. covtype$Class <- as.factor(covtype$Class) levels(covtype$Class) <- c("Spruce/Fir", "Lodgepole Pine", "Ponderosa Pine","Cottonwood/Willow","Aspen", "Douglas-fir","Krummholz") # How does it looks like? str(covtype) ## ----covtype_wilder1----------------------------------------------------- countWA <- covtype$WA_RWA + covtype$WA_NWA + covtype$WA_CPWA + covtype$WA_CLPWA paste(min(countWA),max(countWA)) ## ----covtype_soil1------------------------------------------------------- soilT <- sprintf("ST%02d",1:40) countST <- rowSums(covtype[,soilT]) paste(min(countST),max(countST)) ## ----covtype_convert1---------------------------------------------------- # Which are the columns we want to consider? searchOn <- c("WA_RWA","WA_NWA","WA_CPWA","WA_CLPWA") # Get the index of each WA_ indexOfWA <- apply(covtype[,searchOn], 1, function(x) which(x == 1)) # Convert it to a factor with the column names we used. factorOfWA <- factor(indexOfWA,labels = searchOn) # Add it to the data frame covtype$WildArea <- factorOfWA # Drop the binary variables we don't need anymore covtype[searchOn] <- list(NULL) ## ----covtype_convert2---------------------------------------------------- # Which are the columns we want to consider? searchOn <- sprintf("ST%02d",1:40) searchOn # Get the index of 1 in ST01..ST40 indexOfST <- apply(covtype[,searchOn], 1, function(x) which(x == 1)) # Convert it to a factor with the column names we used. factorOfST <- factor(indexOfST,labels = searchOn) # Add it to the data frame covtype$SoilType <- factorOfST # Drop the binary variables we don't need anymore covtype[searchOn] <- list(NULL) str(covtype) ## ----covtype_summary----------------------------------------------------- summary(covtype) # Let's save it just in case! save(covtype,file="Data/CoverageType/CovType2.R") ## ----covtype_boxplot1, fig.width=7, fig.height=7------------------------- par(mfrow=c(1,3),mar=c(6,3,2,1)) boxplot(covtype$Elevation, main="Elevation",las=2) boxplot(covtype$Slope, main="Slope",las=2) boxplot(covtype$HorDistFire, main="Hor.Dist.Fire",las=2) ## ----covtype_boxplot2, fig.width=7, fig.height=7------------------------- par(mfrow=c(1,3),mar=c(6,3,2,1)) boxplot(covtype$HorDistToHydro, main="Hor.Dist.Hydro",las=2) boxplot(covtype$VertDistToHydro, main="Vert.Dist.Hydro",las=2) boxplot(covtype$HorDistRoad, main="Hor.Dist.Road",las=2) ## ----covtype_viol1, fig.width=7, fig.height=7---------------------------- library(beanplot) beanplot(covtype$HorDistToHydro,covtype$HorDistRoad,covtype$HorDistFire, names=c("Hor.Dist.Hydro","Hor.Dist.Road","Hor.Dist.Fire")) ## ----covtype_edatable1, results='asis'----------------------------------- library(xtable) tabSW <- table(covtype$SoilType,covtype$WildArea) print(xtable(tabSW), type="html") ## ----covtype_edatable2, results='asis'----------------------------------- tabCW <- table(covtype$Class,covtype$WildArea) print(xtable(tabCW), type="html") ## ----covtype_edatable1v, fig.width=7, fig.height=8----------------------- colfunc <- colorRampPalette(c("red", "yellow","green","cyan","blue","magenta")) mosaicplot(t(tabSW),main="Soil Type x Wilderness Area", ylab="Soil Type",xlab="Wilderness Area",col=colfunc(nlevels(covtype$SoilType))) ## ----covtype_edatable2v, fig.width=7, fig.height=7----------------------- mosaicplot(t(tabCW),main="Class x Wilderness Area", ylab="Class",xlab="Wilderness Area",col=colfunc(nlevels(covtype$Class)))