merge.data.SWISS.R 5.09 KiB
### MERGE Swiss DATA Edited by FH
rm(list = ls())
source("./R/format.function.R")
library(reshape)
library(foreign)
######################### READ DATA read individuals tree data
data.swiss1 <- read.csv("./data/raw/DataSwiss/LFI12.csv", header = TRUE, stringsAsFactors = FALSE)
data.swiss2 <- read.csv("./data/raw/DataSwiss/LFI23.csv", header = TRUE, stringsAsFactors = FALSE)
data.swiss3 <- read.csv("./data/raw/DataSwiss/LFI34.csv", header = TRUE, stringsAsFactors = FALSE)
data.swiss <- rbind(data.swiss1, data.swiss2, data.swiss3)
rm(data.swiss1, data.swiss2, data.swiss3)
data.swiss <- data.swiss[order(data.swiss$BANR), ]
data.swiss <- data.swiss[, c("INVNR", "CLNR", "BANR", "X", "Y", "BAUMART", "TEXT", 
    "VEGPER", "BHD1", "BHD2", "BA1", "BA2", "BAI", "BHD_DIFF", "RPSTZ1", "RPSTZ2", 
    "HOEHE1", "HOEHE2")]
colnames(data.swiss) <- c("Inventid", "siteid", "treeid", "x", "y", "spcode", "sp.name", 
    "year", "dbh1", "dbh2", "ba1", "ba2", "ba_diff", "dbh_diff", "repfactor1", "repfactor2", 
    "ht1", "ht2")
## Do not need to read in spp list as it is already available in data.swiss
###################################### 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
res.quant.boot <- t(sapply(levels(factor(data.swiss[["spcode"]])), FUN = f.quantile.boot, 
    R = 1000, x = log10(apply(data.swiss[, c("ht1", "ht2")], 1, max, na.rm = T)), 
    fac = factor(data.swiss[["spcode"]])))
## 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])
rm(res.quant.boot)
# write.csv(data.max.height,file='./data/process/data.max.height.swiss.csv')
########################################## FORMAT INDIVIDUAL TREE DATA change unit and names of variables to be the same
########################################## in all data for the tree
data.swiss$G <- 10 * (data.swiss$dbh_diff)/data.swiss$year  ## diameter growth in mm per year - SOME EXTREMELY NEGATIVE HERE!
data.swiss$D <- data.swiss[["dbh1"]]
data.swiss$D[data.swiss$D == 0] <- NA
## diameter in cm
data.swiss$dead <- rep(NA, length(data.swiss[["dbh1"]]))  ## Mortality - MISSING
data.swiss$plot <- data.swiss$siteid  ## plot code
data.swiss$htot <- data.swiss$ht1  ## height of tree in m 
###################### ECOREGION Ecoregion not available for swiss data
data.swiss$ecocode <- rep("A", nrow(data.swiss))
###################### PERCENT DEAD variable percent dead/cannot do with since dead variable is
###################### missing compute numer of dead per plot to remove plot with disturbance
perc.dead <- tapply(data.swiss[["dead"]], INDEX = data.swiss[["plot"]], FUN = function.perc.dead)
# ## VARIABLE TO SELECT PLOT WITH NOT BIG DISTURBANCE KEEP OFTHER VARIABLES IF
# AVAILABLE (disturbance record)
data.swiss <- merge(data.swiss, data.frame(plot = names(perc.dead), perc.dead = perc.dead), 
    by = "plot", sort = FALSE)
########################################################### PLOT SELECTION FOR THE ANALYSIS Remove data with dead == 1
table(data.swiss$dead)
## Nothing to remove
data.climate <- read.dbf(file = "./data/raw/DataSwiss/LFI14_climate.dbf")
data.climate <- data.climate[, c(1, 7, 15:19)]
data.climate$MAP <- apply(data.climate[, 4:7], 1, sum)
data.swiss <- merge(data.swiss, data.frame(siteid = data.climate$CLNR, swb = data.climate$swb_100, 
    MAT = data.climate$tave_68, MAP = data.climate$MAP), sort = F, all.x = T)
rm(data.climate)
############################################## COMPUTE MATRIX OF COMPETITION INDEX WITH SUM OF BA PER SPECIES IN EACH PLOT in
############################################## m^2/ha without the target species
7172737475767778798081828384858687888990919293949596
data.BA.SP <- BA.SP.FUN(id.tree = as.vector(data.swiss[["treeid"]]), diam = as.vector(data.swiss[["D"]]), sp = as.vector(data.swiss[["spcode"]]), id.plot = as.vector(data.swiss[["siteid"]]), weights = data.swiss[["repfactor1"]], weight.full.plot = NA) ## change NA and <0 data for 0 data.BA.SP[is.na(data.BA.SP)] <- 0 data.BA.SP[, -1][data.BA.SP[, -1] < 0] <- 0 ### CHECK IF sp and sp name for column are the same if (sum(!(names(data.BA.SP)[-1] %in% unique(data.swiss[["spcode"]]))) > 0) stop("competition index sp name not the same as in data.tree") #### compute BA tot for all competitors BATOT.COMPET <- apply(data.BA.SP[, -1], 1, sum, na.rm = TRUE) data.BA.SP$BATOT.COMPET <- BATOT.COMPET rm(BATOT.COMPET) ### create data frame names(data.BA.SP) <- c("tree.id", names(data.BA.SP)[-1]) data.BA.sp <- merge(data.frame(tree.id = data.swiss[["tree.id"]], ecocode = data.swiss[["ecocode"]]), data.BA.SP, by = "tree.id", sort = FALSE) ## test if (sum(!data.BA.SP[["treeid"]] == data.swiss[["treeid"]]) > 0) stop("competition index not in the same order than data.tree") ## save everything as a list list.swiss <- list(data.tree = data.tree, data.BA.SP = data.BA.sp, data.traits = data.traits) save(list.swiss, file = "./data/process/list.swiss.Rdata")