Paracou.R 6.5 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
19
20
colnames(data.paracou) <- c("forest","plot","subplot","tree","vernacular","taxonid","x","y",
                            "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
28
data.paracou$tree.id <- apply(data.paracou[,c("plot","subplot","tree")],1,paste,collapse="_");
data.paracou$sp <- data.paracou[["taxonid"]]
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")
Georges Kunstler's avatar
Georges Kunstler committed
32
## lapply(unique(data.paracou[["plot"]]),FUN=fun.circles.plot,data.paracou[['x']],data.paracou[['y']],data.paracou[["plot"]],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
39
## 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
data.paracou <- subset(data.paracou,subset=! data.paracou[["plot"]] %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
47
48
49
50
51
52
species.clean$sp <- species.clean[["idTaxon"]]
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
data.paracou$census <- data.paracou$yr1
Georges Kunstler's avatar
merged    
Georges Kunstler committed
76
data.paracou$D <- data.paracou[["dbh1"]]; data.paracou$D[data.paracou$D == 0] <- NA ;## diameter in cm
77
data.paracou$plot <- paste("p",data.paracou$plot,sep=".")#apply(data.paracou[,c("forest","plot","subplot")],1,paste,collapse=".") ## plot code
78
data.paracou$htot <- rep(NA,length(data.paracou[["G"]])) ## height of tree in m
79
data.paracou$obs.id <- 1:nrow(data.paracou)
Georges Kunstler's avatar
Georges Kunstler committed
80
data.paracou$subplot <-  paste(data.paracou$plot,data.paracou$subplot)
Georges Kunstler's avatar
Georges Kunstler committed
81
### delete recruit in 2001 or 2005 for first census
Georges Kunstler's avatar
merged    
Georges Kunstler committed
82
data.paracou <- subset(data.paracou,subset=!is.na(data.paracou$D))
Georges Kunstler's avatar
Georges Kunstler committed
83
## minimum circumfer 30 delete all tree with a dbh <30/pi,
Georges Kunstler's avatar
merged    
Georges Kunstler committed
84
data.paracou <-  subset(data.paracou,subset= data.paracou[["D"]]>(30/pi))
Georges Kunstler's avatar
Georges Kunstler committed
85
86
87
88
89
## 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


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


###################### 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.paracou, select = c(vec.basic.var))
98
99
100
101
102

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

103
104
105
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))
data.tree <- subset(data.tree,subset=data.tree$census==2005)


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