Paracou.R 7.36 KB
Newer Older
Daniel Falster's avatar
Daniel Falster committed
1
2
#!/usr/bin/env Rscript

Georges Kunstler's avatar
merged    
Georges Kunstler committed
3
### MERGE paracou DATA
Georges Kunstler's avatar
Georges Kunstler committed
4

Georges Kunstler's avatar
Georges Kunstler committed
5
rm(list = ls())
6
7
source("R/format.data/format.fun.R")
dir.create("output/formatted/Paracou", recursive=TRUE,showWarnings=FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
8
9
library(reshape,quietly=TRUE)

10
############################ read individuals tree data
11
data.paracou <- read.table("data/raw/Paracou/20130717_paracou_1984_2012.csv",
Georges Kunstler's avatar
merged    
Georges Kunstler committed
12
13
                           header=TRUE,stringsAsFactors=FALSE,sep = ";", na.strings = "NULL")
#barplot(apply(!is.na(data.paracou[,paste("circ_",1984:2012,sep="")]),MARGIN=2,FUN=sum),las=3)
Georges Kunstler's avatar
Georges Kunstler committed
14
# select good columns
Georges Kunstler's avatar
merged    
Georges Kunstler committed
15
16
17
data.paracou <- data.paracou[,c("foret","parcelle","carre","arbre","vernaculaire","idtaxon",
                                "x","y","circ_2001","code_2001","circ_2005","code_2005",
                                "circ_2009","code_2009","campagne_mort","type_mort")]
18
colnames(data.paracou) <- c("forest","cluster","plot","tree","vernacular","taxonid","x","y",
19
20
                            "circum2001","code2001","circum2005","code2005","circum2009",
                            "code2009","yeardied","typedeath")
Georges Kunstler's avatar
Georges Kunstler committed
21

22
### change numeric separator
Georges Kunstler's avatar
merged    
Georges Kunstler committed
23
24
25
26
numeric.col.name <-   c("x","y","circum2001","code2001","circum2005","code2005","circum2009","code2009")
for(k in numeric.col.name){ 
	data.paracou[,k] <- gsub(",",".",data.paracou[,k]); data.paracou[,k] <- as.numeric(data.paracou[,k])
    } ## Replace all , in decimals with .
27
data.paracou$tree.id <- apply(data.paracou[,c("cluster","plot","tree")],1,paste,collapse="_");
Georges Kunstler's avatar
Georges Kunstler committed
28
data.paracou$sp <- paste("sp",data.paracou[["taxonid"]],sep=".")
Georges Kunstler's avatar
merged    
Georges Kunstler committed
29
data.paracou <- data.paracou[,c(ncol(data.paracou),1:(ncol(data.paracou)-1))]	
Georges Kunstler's avatar
Georges Kunstler committed
30
## plot each plot
31
## pdf("output/figs/plots.paracou.pdf")
32
## lapply(unique(data.paracou[["cluster"]]),FUN=fun.circles.plot,data.paracou[['x']],data.paracou[['y']],data.paracou[["cluster"]],data.paracou[["circum2009"]],inches=0.2,fg.l=data.paracou$subplot)
33
## dev.off()
Georges Kunstler's avatar
Georges Kunstler committed
34
35
## #
############################ SELECT OBSERVATION WITHOUT PROBLEMS
Georges Kunstler's avatar
merged    
Georges Kunstler committed
36
37
38
## REMOVE ALL TREES WITH X OR Y >250 m 
data.paracou <- subset(data.paracou,subset=(!is.na(data.paracou[["x"]])) & data.paracou[["x"]]<251 &  data.paracou[["y"]]<251)
#### REMOVE PLOTs 16 17 18 ACCORDING TO  GHSILAIN
39
data.paracou <- subset(data.paracou,subset=! data.paracou[["cluster"]] %in% 16:18)
40
## keep only tree alive in 2001
Georges Kunstler's avatar
merged    
Georges Kunstler committed
41
data.paracou <- subset(data.paracou,subset=!(as.numeric(data.paracou[["yeardied"]])<=2001 & !is.na(data.paracou[["yeardied"]])))
42
43
######################################## MASSAGE TRAIT DATA
### read species names
Georges Kunstler's avatar
Georges Kunstler committed
44

45
species.clean <- read.csv("data/raw/Paracou/20130717_paracou_taxonomie.csv",stringsAsFactors=FALSE, header = T, sep = ";")
46
species.clean$sp <- paste("sp.",species.clean[["idTaxon"]],sep="")
47
48
49
50
51
52
species.clean$Latin_name <-  paste(species.clean[["Genre"]],species.clean[["Espece"]],sep=" ")
## keep only one row pers idTaxon
species.clean <- subset(species.clean,subset=!duplicated(species.clean[["sp"]]),select=c("sp","Latin_name","Genre","Espece","Famille","idCIRAD"))
## select only species present in data base
species.clean <-  subset(species.clean,subset=species.clean[["sp"]] %in% data.paracou[["sp"]])
## percentage of species with no taxonomic identification 
Georges Kunstler's avatar
Georges Kunstler committed
53
54
55
56
length(grep("Indet",species.clean[["Latin_name"]]))/nrow(species.clean) ## 25% agree with Bruno ?



57
############################################ FORMAT INDIVIDUAL TREE DATA
Georges Kunstler's avatar
merged    
Georges Kunstler committed
58
59
data.paracou2 <- data.paracou[rep(1:nrow(data.paracou),each=2),c(1:10,(ncol(data.paracou)-2):ncol(data.paracou))]
rownames(data.paracou2) <- 1:nrow(data.paracou2); data.paracou2 <- as.data.frame(data.paracou2)
60
data.paracou2$yr1 <- rep(c(2001,2001+4),nrow(data.paracou)); data.paracou2$yr2 <- rep(c(2005,2005+4),nrow(data.paracou))
Georges Kunstler's avatar
merged    
Georges Kunstler committed
61
62
63
64
65
66
67
68
data.paracou2$year <- rep(c(4,4),nrow(data.paracou))
data.paracou2$dbh1 <- c(rbind(data.paracou$circum2001/pi,data.paracou$circum2005/pi))
data.paracou2$dbh2 <- c(rbind(data.paracou$circum2005/pi,data.paracou$circum2009/pi))
data.paracou2$code1 <- c(as.numeric(rbind(data.paracou$code2001,data.paracou$code2005)))
data.paracou2$code2 <- c(as.numeric(rbind(data.paracou$code2005,data.paracou$code2009)))
data.paracou2$dead <- rep(0,nrow(data.paracou)*2)
data.paracou2$dead[c(as.numeric(data.paracou[["yeardied"]]) %in% 2002:2005 & (!is.na(data.paracou[["yeardied"]])),
                     as.numeric(data.paracou[["yeardied"]]) %in% 2006:2009 & (!is.na(data.paracou[["yeardied"]])))] <- 1
69
## remove tree dead at first census for both date (census 2001-2005 2005-2009)
Georges Kunstler's avatar
merged    
Georges Kunstler committed
70
71
72
73
74
data.paracou <- subset(data.paracou2,subset=!(data.paracou2[['yr1']] ==2005 & (as.numeric(data.paracou[["yeardied"]]) %in% 2002:2005 & (!is.na(data.paracou[["yeardied"]])))))
## change unit and names of variables to be the same in all data for the tree 
data.paracou$G <- 10*(data.paracou$dbh2-data.paracou$dbh1)/data.paracou$year ## diameter growth in mm per year
data.paracou$G[data.paracou$code1>0] <- NA ## indivs with code indicating problem in dbh measurment at dbh1
data.paracou$G[data.paracou$code2>0] <- NA ## indivs with code indicating problem in dbh measurment at dbh2
Georges Kunstler's avatar
Georges Kunstler committed
75
76
77
data.paracou$BA.G <- (pi*(data.paracou$dbh2/2)^2-pi*(data.paracou$dbh1/2)^2)/data.paracou$year ## BA growth in cm2 per year
data.paracou$BA.G[data.paracou$code1>0] <- NA ## indivs with code indicating problem in dbh measurment at dbh1
data.paracou$BA.G[data.paracou$code2>0] <- NA ## indivs with code indicating problem in dbh measurment at dbh2
Georges Kunstler's avatar
Georges Kunstler committed
78
data.paracou$census <- data.paracou$yr1
Georges Kunstler's avatar
merged    
Georges Kunstler committed
79
data.paracou$D <- data.paracou[["dbh1"]]; data.paracou$D[data.paracou$D == 0] <- NA ;## diameter in cm
80
data.paracou$cluster <- paste("p",data.paracou$cluster,sep=".")#apply(data.paracou[,c("forest","cluster","plot")],1,paste,collapse=".") ## plot code
81
data.paracou$htot <- rep(NA,length(data.paracou[["G"]])) ## height of tree in m
82
data.paracou$obs.id <- 1:nrow(data.paracou)
83
data.paracou$plot <-  paste(data.paracou$cluster,data.paracou$plot)
Georges Kunstler's avatar
Georges Kunstler committed
84
### delete recruit in 2001 or 2005 for first census
Georges Kunstler's avatar
merged    
Georges Kunstler committed
85
data.paracou <- subset(data.paracou,subset=!is.na(data.paracou$D))
Georges Kunstler's avatar
Georges Kunstler committed
86
## minimum circumfer 30 delete all tree with a dbh <30/pi,
Georges Kunstler's avatar
merged    
Georges Kunstler committed
87
data.paracou <-  subset(data.paracou,subset= data.paracou[["D"]]>(30/pi))
Georges Kunstler's avatar
Georges Kunstler committed
88
89
90
91
92
## add latin name
data.paracou <- merge(data.paracou,subset(species.clean,select=c("sp","Latin_name")),by="sp")
data.paracou$sp.name <- data.paracou$Latin_name


93
################################################## VARIABLES SELECTION FOR THE ANALYSIS
Georges Kunstler's avatar
merged    
Georges Kunstler committed
94
#vec.abio.var.names <-  c("MAT","MAP") ## MISSING NEED OTHER BASED ON TOPOGRAPHY ASK BRUNO
Georges Kunstler's avatar
Georges Kunstler committed
95
96
97


###################### PLOT SELECTION FOR THE ANALYSIS
Georges Kunstler's avatar
Georges Kunstler committed
98
vec.basic.var <- c("obs.id","tree.id", "sp", "sp.name","cluster", "plot", "D", "G","BA.G", "dead", 
Georges Kunstler's avatar
Georges Kunstler committed
99
100
    "year", "htot", "x", "y", "census")
data.tree <- subset(data.paracou, select = c(vec.basic.var))
101
102
103
104
105

data.tree[["Lon"]] <- -52.900002
data.tree[["Lat"]] <- 5.38
data.tree[["ecocode"]] <- "tropical"

106
107
## select tree above 10 cm and last census only
data.tree <- subset(data.tree,subset=data.tree$D>10 & !is.na(data.tree$D))
108
## data.tree <- subset(data.tree,subset=data.tree$census==2005)
109

110
data.tree <- subset(data.tree,subset=!is.na(data.tree$x) & !is.na(data.tree$y))
111

112
write.csv(data.tree,file="output/formatted/Paracou/tree.csv",row.names = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
113

Georges Kunstler's avatar
Georges Kunstler committed
114
115
116
117
118
### write data plot with variables only at the plot level. I should delete them from the tree table but so far I keep them to not distroy later code

vec.basic.var.p <- c("plot", "cluster", "Lon","Lat","ecocode")
data.plot <- subset(data.tree, subset=!duplicated(data.tree$cluster),select = c(vec.basic.var.p))
write.csv(data.plot,file="output/formatted/Paracou/plot.csv",row.names = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
119