Commit a1ab162e authored by fhui28's avatar fhui28
Browse files

template Sweden R script; major formatting issues remain

No related merge requests found
Showing with 51 additions and 33 deletions
+51 -33
### MERGE sweden DATA
rm(list = ls()); source("./R/format.function.R"); library(reshape);
#########################
## READ DATA
####################
### read individuals tree data
######################### READ DATA read individuals tree data
#data.swe1 <- read.csv("./data/raw/DataSweden/Swe_NFI_1.csv",header=T,stringsAsFactors=F)
#data.swe2 <- read.csv("./data/raw/DataSweden/Swe_NFI_2a.csv",header=T,stringsAsFactors=F)
#data.swe3 <- read.csv("./data/raw/DataSweden/Swe_NFI_3.csv",header=T,stringsAsFactors=F)
data.swe <- read.table("./data/raw/DataSweden/Swe_NFI_all.txt",header=T,stringsAsFactors=F,sep="\t")
head(data.swe)
data.swe$plot.id <- paste(data.swe[["TractNr"]],data.swe[["PlotNr"]],data.swe[["Year"]])
data.swe$tree.id <- paste(data.swe[["TreeNr"]],data.swe[["TractNr"]],data.swe[["PlotNr"]],data.swe[["Year"]])
### Species names are in the xlsx files if required (we already have sp codes)
data.swe$tree.id <- apply(cbind(data.swe[["TreeNr"]],data.swe[["TractNr"]],data.swe[["PlotNr"]],data.swe[["Year"]]),1,paste,collapse="_")
data.swe$plot.id <- apply(cbind(data.swe[["TractNr"]],data.swe[["PlotNr"]],data.swe[["Year"]]),1,paste,collapse="_")
#data.swe <- data.swe[order(data.swe$tree.id),]
## Species names are in the xlsx files if required (we already have sp codes)
#dim(data.swe)
#table(table(data.swe$TreeID))
#table(table(paste(data.swe$TractNr,data.swe$PlotNr,data.swe$TreeNr,data.swe$Year)))
#data.swe <- rbind(data.swe1, data.swe2, data.swe3);
#rm(data.swe1, data.swe2, data.swe3)
#data.swe$treeid <- apply(data.swe[,3:5],1,paste,collapse="_")
#data.swe$plotid <- apply(data.swe[,3:4],1,paste,collapse="_")
data.swe <- data.swe[order(data.swe$TreeID,data.swe$PlotInvent),] ## Shows the TreeID = "" first
data.swe <- data.swe[order(paste(data.swe$TractNr,data.swe$PlotNr,data.swe$TreeNr)),] ## Shows the TreeID = "" first
head(data.swe)
sum(data.swe$TreeID == "")
dim(data.swe)
table(table(data.swe$TreeID))
table(table(paste(data.swe$TractNr,data.swe$PlotNr,data.swe$TreeNr,data.swe$Year)))
### STOP HERE!!!
######################################
## MASSAGE TRAIT DATA
############################
## Mean height in dataset
##########################################
## FORMAT INDIVIDUAL TREE DATA
#############
## change unit and names of variables to be the same in all data for the tree
data.swe$G <- 10*(data.swe$FinalDBH-data.swe$InitDBH)/data.swe$Interval ## diameter growth in mm per year
#data.swe <- data.swe[order(data.swe$TreeID,data.swe$PlotInvent),] ## Shows the TreeID = "" first
#data.swe <- data.swe[order(paste(data.swe$TractNr,data.swe$PlotNr,data.swe$TreeNr)),] ## Shows the TreeID = "" first
## Format to desired form
data.swe$PlotInventID <- NULL; data.swe$TreeID <- NULL;
data.swe2 <- data.swe[rep(1:nrow(data.swe),each=2),]
data.swe2$dbh1 <- as.vector(rbind(data.swe$dia_t1,data.swe$dia_t2))
data.swe2$dbh2 <- as.vector(rbind(data.swe$dia_t2,data.swe$dia_t3))
data.swe2$dia_t1 <- data.swe2$dia_t2 <- data.swe2$dia_t3 <- NULL
data.swe2$vol1 <- as.vector(rbind(data.swe$vol_t1,data.swe$vol_t2))
data.swe2$vol2 <- as.vector(rbind(data.swe$vol_t2,data.swe$vol_t3))
data.swe2$vol_t1 <- data.swe2$vol_t2 <- data.swe2$vol_t3 <- NULL
data.swe2$dryw1 <- as.vector(rbind(data.swe$DryW_t1,data.swe$DryW_t2))
data.swe2$dryw2 <- as.vector(rbind(data.swe$DryW_t2,data.swe$DryW_t3))
data.swe2$DryW_t1 <- data.swe2$DryW_t2 <- data.swe2$DryW_t3 <- NULL
data.swe2$Diameter <- data.swe2$Volume <- data.swe2$BrhAge <- NULL
data.swe <- data.swe2
rm(data.swe2)
###################################### MASSAGE TRAIT DATA Compute maximum height per species plus sd from observed
###################################### height to add variables to the traits data base Because we have two heights,
###################################### then take the max of the two heights and then bootstrap
## Obtain heights in dataset - I assume that they use the volume of a cylinder V = pi*r^2*h
data.swe$ht1 <- data.swe$vol1/(pi*(0.5*data.swe$dbh1/1000)^2)
data.swe$ht2 <- data.swe$vol2/(pi*(0.5*data.swe$dbh2/1000)^2)
res.quant.boot <- t(sapply(levels(factor(data.swe[["TreeSpecies"]])), FUN = f.quantile.boot,
R = 1000, x = log10(apply(data.swe[, c("ht1", "ht2")], 1, max, na.rm = T)),
fac = factor(data.swe[["TreeSpecies"]])))
## create data base
data.max.height <- data.frame(code = rownames(res.quant.boot), Max.height.mean = res.quant.boot[,
1], Max.height.sd = res.quant.boot[, 2], Max.height.nobs = res.quant.boot[, 3], stringsAsFactors =FALSE)
rm(res.quant.boot)
write.csv(data.max.height,file='./data/process/data.max.height.swe.csv')
########################################## FORMAT INDIVIDUAL TREE DATA
## STOP HERE!!!
data.swe$obs.id <- 1:nrow(data.swe)
data.swe$census.id <- rep(1:2,nrow(data.swe)/2) ## Using first census in the row
data.swe$G <- 10*(data.swe$dbh2-data.swe$dbh1)/data.swe$Interval ## diameter growth in mm per year
data.swe$G[which(data.swe$InitDBH == 0 | data.swe$FinalDBH == -999)] <- NA
data.swe$year <- data.swe$Interval ## number of year between measuremen
data.swe$D <- data.swe[["InitDBH"]]; data.swe$D[data.swe$D == 0] <- NA ;## diameter in cm
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment