## Getting the data in order; work with raw data files with tree and plot data, and combine into summarized data by plot (point). ```{r} #plot-level files. All the hot spots, and the subset of plots that were measured. Put together. wd <- ('C:/Users/aabarker/Dropbox (Personal)/GypsyMothOak-Main/Hotspot2017LandsatValidation/HotspotDataArchive') setwd(wd) library(plyr) latlong<-read.csv('LatLong.csv') measplot<-read.csv('MeasuredPoints.csv') spotplots<-merge(measplot,latlong, by = c('hotspot', 'point'), all.x=TRUE) #tree-level measurements file. trees<-read.csv('Trees.csv', header = TRUE) str(trees) treep<-merge(trees, spotplots, by=c('hotspot', 'point'), all.x=TRUE, all.y=TRUE) #trees with exceedingly large dbh #TooBig<-subset(treep, treep$DBH>40) #These 4 trees actually all check out ok with the data sheets. They are just big. #remove NA lines except for the 3 plots that have no trees #how many are there? treepNA<-subset(treep, is.na(treep$DBH)) #remove 6 or 7 lines with DBH NA: 5 are dead trees; 1 is a crossed-out line on the data sheet #fill in mean dbh for the one tree with dbh not recorded. Hot plot 3 50, RO. #mean dbh of the other 6 red oaks in the plot is (14+20+14+16+12+14)6 = 15 inches. #there is one tree (hotplot 3 16) with species = NA but sppgr=other hdwd. Keep. treep$DBH<-ifelse(treep$hotspot==2 & treep$point==50 & treep$line==35, 15, treep$DBH) treep<-subset(treep, !is.na(treep$DBH)) #get diameters into cm treep$dbh.cm<-ifelse(!is.na(treep$DBH),treep$DBH*2.54, 0) summary(treep) #create species groups levels(treep$SPP) treep$sppgr<-ifelse(treep$SPP %in% c('BO','RO','SO','WO','CO','SWO'), 'oak', ifelse(treep$SPP %in% c('WP','HK','RP','NS','LA','RS'), 'conifer','otherhd')) #make a column with defoliation class represented as the mid-point proportion of that class treep$DefProp<-ifelse(treep$RefClss==1, 0.125, ifelse(treep$RefClss==2, 0.375, ifelse(treep$RefClss==3, 0.625, 0.875))) #make a column with a unique plot ID treep$hotplot<-paste(treep$hotspot, treep$point) #save this clean version of tree data write.csv(treep, '2017HotSpotTrees.csv',row.names=FALSE) ## Summarize by Plot ## ## Some clean-up. plotBAdef<-ddply(subset(treep, !is.na(treep$dbh.cm)), c('hotspot','point', 'hotplot' ,'type'), summarise, BAft2acre=length(point)*10, n.trees=length(point), MeanDef=mean(DefProp, na.rm=TRUE)) #iszero<-subset(treep, treep$dbh.cm==0) #3 lines have dbh=0. They represent plots with BA=0 (recent harvest) #force the no-tree plots from 10 to 0 BA plotBAdef$BAft2acre<-ifelse(plotBAdef$BAft2acre==10, 0, plotBAdef$BAft2acre) #yes, this pulls out only those 3 plots with no trees plotBAdefno0<- subset(plotBAdef, !is.na(plotBAdef$MeanDef)) BApointspdef<-ddply(treep, c('hotspot','point', 'hotplot', 'sppgr'), summarise, BAft2acre=length(point)*10, MeanDef=mean(DefProp, na.rm=TRUE)) # takes care of missing obs for one tree each in 1 7 and 3 4 BApointspdefno0<- subset(BApointspdef, !is.na(BApointspdef$MeanDef)) library(reshape2) wspec <- reshape(BApointspdefno0, v.names = c("BAft2acre","MeanDef"), idvar = c('hotplot'), timevar = "sppgr", direction = "wide") together<-cbind(plotBAdefno0, wspec) together[is.na(together)] <- 0 #make sure this is correct together$okay<-(together$BAft2acre.conifer+together$BAft2acre.oak+together$BAft2acre.otherhd) together$test<-together$BAft2acre-together$okay summary(together) #add proportion oak and clean up the data frame together$PropOak<-together$BAft2acre.oak/together$BAft2acre HotPlotMeans<-together[, c(1:7, 11:16, 19)] write.csv(HotPlotMeans, 'HotPlotMeans.csv', row.names = FALSE) ```