volume weighted trait means
# -- DIA weighted plot trait means
# !!! make sure all traits are saved as numbers instead of factors !!!
setwd("F:/Data/projects/DGSII/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
plotvol=read.csv("plot_spp_vol_wt.csv", header=TRUE)
data2=merge(plotvol,traits,all.x=TRUE)
env=read.csv("plots_env.csv", header=TRUE)
plotList=unique(data1$PLOT)
head(plotvol)
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,"volweighted_plottraitmean_5-19.csv")
head(result)
envplot=merge(env,result, by.x = "PLOT", by.y = "plotList")
head(envplot)
write.csv(envplot,"envplot_volweight_5-19.csv")