diff --git a/merge.data.NSW.R b/merge.data.NSW.R index 76b800942b2681273d243c2132687390508592b3..de963b99bfaee65763fe26e2431115dc6b463a6e 100644 --- a/merge.data.NSW.R +++ b/merge.data.NSW.R @@ -52,24 +52,24 @@ data.traits <- read.csv("./data/raw/DataNSW/NSW_traits.csv", header = TRUE, stri ########################################## FORMAT INDIVIDUAL TREE DATA Each tree has at most 3 observations (from prelim ########################################## checks of the data) -data.nsw$treeid <- apply(data.nsw[, 1:2], 1, paste, collapse = ".") +data.nsw$treeid <- apply(data.nsw[, 1:2], 1, paste, collapse = "_") data.nsw2 <- data.frame(data.nsw[1, ], year1 = NA, year2 = NA, dbh1 = NA, dbh2 = NA) for (k in 1:length(unique(data.nsw$treeid))) { sub.datansw <- as.data.frame(data.nsw[which(data.nsw$treeid == unique(data.nsw$treeid)[k]), ]) if (nrow(sub.datansw) == 1) { data.nsw2 <- rbind(data.nsw2, data.frame(sub.datansw, year1 = sub.datansw$Date.of.measure[1], - year2 = NA, dbh1 = sub.datansw$Dbh[1], dbh2 = NA)) + year2 = NA, dbh1 = sub.datansw$Dbh[1], dbh2 = NA, stringsAsFactors = F)) } if (nrow(sub.datansw) == 2) { data.nsw2 <- rbind(data.nsw2, data.frame(sub.datansw[1, ], year1 = sub.datansw$Date.of.measure[1], - year2 = sub.datansw$Date.of.measure[2], dbh1 = sub.datansw$Dbh[1], dbh2 = sub.datansw$Dbh[2])) + year2 = sub.datansw$Date.of.measure[2], dbh1 = sub.datansw$Dbh[1], dbh2 = sub.datansw$Dbh[2], stringsAsFactors = F)) } if (nrow(sub.datansw) == 3) { data.nsw2 <- rbind(data.nsw2, data.frame(sub.datansw[1, ], year1 = sub.datansw$Date.of.measure[1], - year2 = sub.datansw$Date.of.measure[2], dbh1 = sub.datansw$Dbh[1], dbh2 = sub.datansw$Dbh[2])) + year2 = sub.datansw$Date.of.measure[2], dbh1 = sub.datansw$Dbh[1], dbh2 = sub.datansw$Dbh[2], stringsAsFactors = F)) data.nsw2 <- rbind(data.nsw2, data.frame(sub.datansw[1, ], year1 = sub.datansw$Date.of.measure[2], - year2 = sub.datansw$Date.of.measure[3], dbh1 = sub.datansw$Dbh[2], dbh2 = sub.datansw$Dbh[3])) + year2 = sub.datansw$Date.of.measure[3], dbh1 = sub.datansw$Dbh[2], dbh2 = sub.datansw$Dbh[3], stringsAsFactors = F)) } } data.nsw2 <- data.nsw2[-1, ] @@ -80,13 +80,11 @@ for (k in 9:12) data.nsw[, k] <- as.numeric(data.nsw[, k]) ## change unit and names of variables to be the same in all data for the tree data.nsw$year <- (data.nsw$year2 - data.nsw$year1) ## number of year between measurements data.nsw$G <- 10 * (data.nsw$dbh2 - data.nsw$dbh1)/(data.nsw$year) ## diameter growth in mm per year -## THERE ARE SOME ROWS WITH STRONG NEGATIVE GROWTH THAT YOU MIGHT WANT TO REMOVE -head(data.nsw[order(data.nsw$G), ]) - -data.nsw$D <- data.nsw[["dbh1"]] -## diameter in cm +#head(data.nsw[order(data.nsw$G), ]) +data.nsw$D <- data.nsw[["dbh1"]] ## diameter in cm data.nsw$dead <- rep(NA, nrow(data.nsw)) ## dummy variable for dead tree 0 alive 1 dead - MISSING -data.nsw$sp <- as.character(data.nsw[["species"]]) ## species code - use the spp name as code +data.nsw$sp <- as.numeric(factor(data.nsw[["species"]])) ## species code - use the spp name as code +data.nsw$sp.name <- data.nsw[["species"]]; data.nsw$species <- NULL data.nsw$plot <- as.character(data.nsw[["Plot"]]) ## plot code data.nsw$htot <- rep(NA, nrow(data.nsw)) ## height of tree in m - MISSING ### add plot weights for computation of competition index (in 1/m^2) - from the @@ -103,11 +101,14 @@ data.nsw$weights[grep("BR", data.nsw$Plot)] <- 1/(60.4 * 60.4) data.nsw$weights[grep("END", data.nsw$Plot)] <- 1/(40 * 50) data.nsw$weights[grep("TND", data.nsw$Plot)] <- 1/(40 * 50) -data.nsw$obs.id <- 1:nrow(data.nsw) +data.nsw$obs.id <- 1:nrow(data.nsw) ## Should change this when census variable is created ###################### ECOREGION nsw has only 1 eco-region ###################### PERCENT DEAD NO DATA ON MORTALITY +perc.dead <- tapply(data.nsw[["dead"]], INDEX = data.nsw[["plot"]], FUN = function.perc.dead2) +data.nsw <- merge(data.nsw, data.frame(plot = names(perc.dead), perc.dead = perc.dead), + by = "plot", sort = FALSE) ########################################### VARIABLES SELECTION FOR THE ANALYSIS vec.abio.var.names <- c("MAT", "MAP") ## MISSING diff --git a/merge.data.SWISS.R b/merge.data.SWISS.R index 58ed741ce5e1f216d1a9e7b5c13da9f6bef899b4..eb7ba34e585922074b14ae36c480002ad88bbb56 100644 --- a/merge.data.SWISS.R +++ b/merge.data.SWISS.R @@ -1,4 +1,4 @@ -### MERGE Swiss DATA Edited by FH +### MERGE Swiss DATA rm(list = ls()) source("./R/format.function.R") source("./R/FUN.TRY.R") @@ -46,14 +46,12 @@ data.swiss$Lat <- coordinates(data.sp2)[, "y"] ## points(data.swiss$x,data.swiss$y) ## rm(data.sp, data.sp2) - - ## 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, +res.quant.boot <- t(sapply(levels(factor(data.swiss[["sp"]])), FUN = f.quantile.boot, R = 1000, x = log10(apply(data.swiss[, c("ht1", "ht2")], 1, max, na.rm = T)), fac = factor(data.swiss[["spcode"]]))) @@ -65,33 +63,32 @@ 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$G <- 10 * (data.swiss$dbh_diff)/data.swiss$year ## diameter growth in mm per year 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$D[data.swiss$D == 0] <- NA ## diameter in cm +data.swiss$dead <- rep(NA, nrow(data.swiss)) ## Mortality - MISSING +data.swiss$plot.id <- data.swiss$siteid; data.swiss$siteid <- NULL ## plot code data.swiss$htot <- data.swiss$ht1 ## height of tree in m data.swiss$census <- data.swiss$Inventid -data.swiss$obs.id <- as.character(1:nrow(data.swiss)) +data.swiss$obs.id <- apply(data.swiss[,c("tree.id","Inventid")],1,paste,collapse="_") data.swiss$weights <- data.swiss$repfactor1/10000 data.swiss$sp <- paste("sp.",data.swiss$sp,sep="") + ###################### 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) +perc.dead <- tapply(data.swiss[["dead"]], INDEX = data.swiss[["plot"]], FUN = function.perc.dead2) # ## VARIABLE TO SELECT PLOT WITH NOT BIG DISTURBANCE KEEP OTHER VARIABLES IF # AVAILABLE (disturbance record) data.swiss <- merge(data.swiss, data.frame(plot = names(perc.dead), perc.dead = perc.dead, stringsAsFactors =FALSE), by = "plot", sort = FALSE) -########################################################### PLOT SELECTION FOR THE ANALYSIS Remove data with dead == 1 -## Nothing to remove - +########################################################### PLOT SELECTION FOR THE ANALYSIS 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.climate <- data.climate[, c("CLNR","swb_100","tave_68","prec_122","prec_35","prec_68","prec_911")] +data.climate$MAP <- apply(data.climate[, c("prec_122","prec_35","prec_68","prec_911")], 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, stringsAsFactors =FALSE), sort = F, all.x = T) @@ -100,7 +97,6 @@ rm(data.climate) ############################################## ############################################## #################### GENERATE ONE OBJECT PER ECOREGION - # vector of ecoregion name ecoregion.unique <- unique(data.swiss[["ecocode"]])