-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_trait_coverage.R
45 lines (28 loc) · 1.32 KB
/
get_trait_coverage.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#' @author Brian Maitner
#' @param wcvp wcvp containing appropriate columns
#' @param trait_summary species, traits, # observations
get_trait_coverage <- function(wcvp, trait_summary, temp_file = "temp/temp_main_trait_coverage.RDS"){
# Set up output object
out <- matrix(ncol = 3,nrow = length(unique(wcvp$area_code_l3))*length(unique(trait_summary$TraitName)))
out <- as.data.frame(out)
colnames(out) <- c("area", "trait", "completeness")
#Two nested for loops
counter <- 1
for(j in 1:length(unique(trait_summary$TraitName))){
for(i in 1:length(unique(wcvp$area_code_l3))){
area_i <- unique(wcvp$area_code_l3)[i]
trait_j <- unique(trait_summary$TraitName)[j]
species_i <- wcvp$taxon_name[which(wcvp$area_code_l3 == area_i)]
out$area[counter] <- area_i
out$trait[counter] <- trait_j
out$completeness[counter] <- length(which(trait_summary$AccSpeciesName %in% species_i & trait_summary$TraitName == trait_j))/length(species_i)
counter <- counter+1
message(round(counter/nrow(out)*100), " percent done")
# save the output after each trait is done
if(i == length(unique(wcvp$area_code_l3))){
saveRDS(object = out, file = temp_file)
}
} # i loop
} # j loop
return(out)
}