R code - DIA (DBH) weighted plot trait means
traits means now weighted by DBH (~biomass) instead of abundance. By JK 5-16. see envplot.csv for the output.
# -- DIA weighted plot trait means
# !!! make sure all traits are saved as numbers instead of factors !!!
setwd("C:/Documents and Settings/kreitler/My Documents/FIA")
data1=read.csv("plot_spp_abun3-28.csv", header=TRUE) #plot spcd #individuals
traits=read.csv("species.traits.csv",header=TRUE) #check column names -- they change, which messes up code
plotdia=read.csv("plot.spp.dia.csv", header=TRUE)
data2=merge(plotdia,traits,all.x=TRUE)
env=read.csv("plots_env.csv", header=TRUE)
plotList=unique(data1$PLOT)
head(plotdia)
head(data1)
head(traits)
head(data2)
# make the matrix before hand so it doesn't bomb
#data2$relativeAbund<-NA
plotseedmass<-c()
plotwoodspecgrav<-c()
plottreetype<-c()
fireresist<-c()
resprout<-c()
sla<-c()
maxheight<-c()
# -- plot level trait weighted mean loop ----
for(i in 1:length(plotList) )
{
temp<- subset(data2, data2$PLOT==plotList[i]) # loop through each plot [i]
#indivs<- sum(temp$CountOfCN) # number of individuals at each plot
#data_rows<-as.integer(row.names(temp)) # ?
#relAbund <-temp$CountOfCN/indivs # relative abundance
#data2$relativeAbund[data_rows] <- relAbund # write relative abundance to data2
plotseedmass[i]<- weighted.mean(temp$seedmass_mg, temp$weight,na.rm = TRUE) #make sure column names are right
plotwoodspecgrav[i]<- weighted.mean(temp$woodspecgrav, temp$weight,na.rm=TRUE)
resprout[i]<- weighted.mean(temp$resprout, temp$weight, na.rm=TRUE)
sla[i]<- weighted.mean(temp$sla,temp$weight,na.rm=TRUE)
plottreetype[i]<- weighted.mean(temp$treetype, temp$weight,na.rm=TRUE)
maxheight[i]<- weighted.mean(temp$maxheight,temp$weight,na.rm=TRUE)
}
result<-data.frame(plotList,plotseedmass,plotwoodspecgrav,resprout,sla,plottreetype,maxheight)
write.csv(result,"DIAweighted_plottraitmean_5-15.csv")