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

Georges Kunstler's avatar
Georges Kunstler committed
3
##### FORMAT TRAIT FOR NVS
4
source("R/find.trait/trait.fun.R")
Georges Kunstler's avatar
Georges Kunstler committed
5
6

### read species names
7
data.tree <- read.csv("output/formatted/NVS/tree.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
8
9
10
11
12
13
14
15
species.clean <- data.frame(sp=data.tree[!duplicated(data.tree[["sp"]]),"sp"],
                             Latin_name=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             Latin_name_syn=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             stringsAsFactors =FALSE)

## delete the sp code with no species
species.clean <- subset(species.clean,subset=!is.na(species.clean$Latin_name))

16
## read in local trait data
Georges Kunstler's avatar
Georges Kunstler committed
17

18
data.trait <- read.csv("data/raw/NVS/nz_traits_130801.csv", , header = TRUE, stringsAsFactors = FALSE)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
data.trait <- data.trait[, -1]
colnames(data.trait)[1] <- "sp"
data.trait$Latin_name <- data.trait$sp
data.trait$Leaf.N.mean <- data.trait$leafn*10; data.trait$leafn <- NULL ## conversion from % to mg/g
data.trait$Leaf.N.sd <- NA ## conversion from % to mg/g
data.trait$Seed.mass.mean <- data.trait$seed.mg; data.trait$seed.mg <- NULL
data.trait$Seed.mass.sd <- NA
data.trait$SLA.mean <- 1/data.trait$lma.gm2; data.trait$SLA.mean <- data.trait$SLA.mean*1000;data.trait$lma.gm2 <- NULL ## conversion of g/m2 to mm2/g1
data.trait$SLA.sd <- NA
data.trait$Wood.density.mean <- data.trait$wood; data.trait$wood <- NULL
data.trait$Wood.density.sd <- NA
data.trait$Max.height.mean <- log10(data.trait$height.m); data.trait$height.m <- NULL
data.trait$Max.height.sd <- NA

## read traits from TRY
data.traits <- fun.extract.format.sp.traits.NOT.TRY(sp=species.clean$sp, Latin_name=species.clean$Latin_name, data=data.trait,name.match.traits="Latin_name")
Georges Kunstler's avatar
Georges Kunstler committed
35
36

### TODO ADD GENUS MEAN FOR HEIGHT IF SPECIES IS MISSING
37
write.csv(data.traits,file="output/formatted/NVS/traits.csv",row.names = FALSE)