An error occurred while loading the file. Please try again.
-
Delaigue Olivier authored3f60ee8c
#!/usr/bin/env Rscript
## options(error = recover)
### MERGE BCI DATA
rm(list = ls())
source("format.fun.R")
dir.create("../../output/formatted/BCI", recursive=TRUE,showWarnings=FALSE)
library(reshape,quietly=TRUE)
############# READ DATA read individuals tree data Requires careful formatting of 7 census
############ datasets The raw data is such that, once a tree dies in census X, then it no
########## longer exists in census X+1, X+2 etc...
data.bci1 <- read.table("../../data/raw/BCI/census1/PlotsDataReport.txt", header = TRUE,
stringsAsFactors = FALSE, sep = "\t")
data.bci1$Date1 <- data.bci1$Date; data.bci1$Date <- NULL
data.bci1$DBH1 <- data.bci1$DBH; data.bci1$DBH <- NULL
big.bci <- NULL
for (k in 2:7) {
new.directory <- paste("../../data/raw/BCI/census", k, "/PlotsDataReport.txt",
sep = "")
data.bci2 <- read.table(new.directory, header = TRUE, stringsAsFactors = FALSE,
sep = "\t");
if (!is.null(big.bci)) {
sub.bci <- merge(data.bci1[, c("Latin","Quadrat","Census","gx","gy","TreeID","Tag","Date1","DBH1")], data.frame(TreeID = data.bci2[["TreeID"]],
DBH2 = data.bci2[["DBH"]], Date2 = data.bci2[["Date"]], dead = as.numeric(data.bci2[["Status"]] ==
"dead"),stringsAsFactors=F), sort = T, by = "TreeID") ## Uses the Date1 as the census number
big.bci <- rbind(big.bci, sub.bci)
}
if (is.null(big.bci)) {
big.bci <- merge(data.bci1[, c("Latin","Quadrat","Census","gx","gy","TreeID","Tag","Date1","DBH1")], data.frame(TreeID = data.bci2[["TreeID"]],
DBH2 = data.bci2[["DBH"]], Date2 = data.bci2[["Date"]], dead = as.numeric(data.bci2[["Status"]] ==
"dead"),stringsAsFactors=F), sort = T, by = "TreeID")
}
data.bci1 <- data.bci2
data.bci1$Date1 <- data.bci1$Date; data.bci1$Date <- NULL
data.bci1$DBH1 <- data.bci1$DBH; data.bci1$DBH <- NULL
cat("Census", k, "now included\n")
# print(summary(big.bci$DBH1)); print(summary(big.bci$DBH2))
}
rm(data.bci1, data.bci2, sub.bci)
big.bci <- big.bci[order(big.bci$TreeID), ]
data.bci <- big.bci
rm(big.bci)
### read species names
species.clean <- read.table("../../data/raw/BCI/TaxonomyDataReport.txt", stringsAsFactors = FALSE,
header = T, sep = "\t")
species.clean$Latin_name <- paste(species.clean[["Genus"]],
species.clean[["species"]],sep=" ")
## ## Try to relate SpeciesID in species.clean species names in data.bci
#unique(data.bci$Latin) %in% species.clean$Latin_name
data.bci <- merge(data.bci, data.frame(Latin = species.clean$Latin_name, sp = species.clean$SpeciesID, genus = species.clean$Genus, stringsAsFactors = F),by = "Latin", sort = F)
########################################## FORMAT INDIVIDUAL TREE DATA
data.bci <- data.bci[order(data.bci[["TreeID"]]),]
data.bci$Date1 <- as.Date(data.bci$Date1)
data.bci$Date2 <- as.Date(data.bci$Date2)
# data.bci$yr1 <- format(strptime(data.bci$Date1, format = '%Y-%m-%d'),'%Y')
# data.bci$yr2 <- format(strptime(data.bci$Date2, format = '%Y-%m-%d'),'%Y')
data.bci$year <- as.numeric(difftime(data.bci$Date2, data.bci$Date1, units = "weeks")/52) ## Not rounded
data.bci$obs.id <- 1:nrow(data.bci) #apply(data.bci[,c("TreeID","Census")],1,paste,collapse="_")
data.bci$tree.id <- data.bci$TreeID; data.bci$TreeID <- NULL
data.bci$x <- data.bci$gx; data.bci$gx <- NULL
data.bci$y <- data.bci$gy; data.bci$gy <- NULL
data.bci$G <- 10 * (data.bci$DBH1 - data.bci$DBH1)/data.bci$year ## diameter growth in mm per year - BASED ON UNROUNDED YEARS
data.bci$D <- data.bci[["DBH1"]]/10 ## diameter in cm
data.bci$subplot <- data.bci[["Quadrat"]]
data.bci$plot <- rep("A",nrow(data.bci))
data.bci$htot <- rep(NA,nrow(data.bci)) ## height of tree in m
data.bci$sp.name <- data.bci$Latin
data.bci$Latin <- NULL
717273747576777879808182
data.bci$census <- data.bci$Census
data.bci$Census <- NULL
data.bci$dead <- as.numeric(is.na(data.bci$DBH2)) ## Not entire sure if this works, given the way this data is designed
###################### PLOT SELECTION FOR THE ANALYSIS
vec.basic.var <- c("obs.id","tree.id", "sp", "sp.name","plot", "subplot", "D", "G", "dead",
"year", "htot", "x", "y", "census")
data.tree <- subset(data.bci, select = c(vec.basic.var))
write.csv(data.tree,file="../../output/formatted/BCI/tree.csv",row.names = FALSE)