-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenBirdDensity.R
164 lines (126 loc) · 6.55 KB
/
genBirdDensity.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
library (dplyr)
library(reshape2)
#################################################################
# Generate Bird Density from ebd Data #
# #
# Param 1: ebd data #
#################################################################
generateBirdDensity <- function(ebd) {
print(nrow(ebd))
if (nrow(ebd) == 0) { return (NULL) }
# Get the complete lists and count the complete lists per range
ebd_lists <- ebd[!duplicated(ebd[c("Submission.ID")]), ]
ebd_complete_lists <- ebd_lists[ebd_lists$All.Obs.Reported == 1,]
ebd_complete_lists_per_range<- dcast(ebd_complete_lists, RANGE ~ ., value.var = "Submission.ID", fun.aggregate = length )
colnames (ebd_complete_lists_per_range) <- c ("Range", "No of Complete Lists")
ebd_all <- ebd
#Remove data from incomplete lists
ebd_all <- ebd_all[ebd_lists$All.Obs.Reported == 1,]
#Remove spuhs
ebd_all <- ebd_all[ebd_all$Category != 'spuh',]
#Remove slashes
ebd_all <- ebd_all[ebd_all$Category != 'slash',]
#Remove subspecies/issf
ebd_all <- ebd_all[!duplicated(ebd_all[c("Genus.Name","Species.Name", "Submission.ID")]),]
#Calculate the encounters per species per range
ebd_density_per_range <- dcast(ebd_all, Taxonomic.Order +
English.India +
Scientific.Name +
IUCN +
WG +
Feeding.Guild +
Raptors +
Primary.Hole.nesters +
Woodland.Understorey.Birds +
Parasitic.Cuckoos ~ RANGE, value.var = "Submission.ID", fun.aggregate = length )
# Dividing the encounters by number of lists for each range.
# Pick the generic columns out, only divide the columns with encounter and then cbind them back.
# Rounding to two decimal places
# Note, the data frame is transposed for division and transposed back
ebd_density_per_range <- cbind (ebd_density_per_range [1:10],
t( round (100 * t(ebd_density_per_range[11:ncol(ebd_density_per_range)]) /
t(ebd_complete_lists_per_range[2])[1:ncol(t(ebd_complete_lists_per_range))], 2)))
# Name the columns properly
colnames (ebd_density_per_range) <- c ("Taxonomic.Order",
"English Name" ,
"Scientific Name",
"IUCN",
"WG",
"Feeding Guild",
"Raptors",
"Primary Hole Nesters",
"Woodland Understorey Birds",
"Parasitic Cuckoos",
colnames(ebd_density_per_range)[11:ncol(ebd_density_per_range)]
)
return (ebd_density_per_range)
}
generateOverallBirdDensity <- function(ebd) {
if(ebd == 0) { return (NULL) }
print(nrow(ebd))
if (nrow(ebd) == 0) { return (NULL) }
# Get the complete lists and count the complete lists per range
ebd_lists <- ebd[!duplicated(ebd[c("Submission.ID")]), ]
ebd_complete_lists <- ebd_lists[ebd_lists$All.Obs.Reported == 1,]
ebd_all <- ebd
#Remove data from incomplete lists
ebd_all <- ebd_all[ebd_lists$All.Obs.Reported == 1,]
#Remove spuhs
ebd_all <- ebd_all[ebd_all$Category != 'spuh',]
#Remove slashes
ebd_all <- ebd_all[ebd_all$Category != 'slash',]
#Remove subspecies/issf
ebd_all <- ebd_all[!duplicated(ebd_all[c("Genus.Name","Species.Name", "Submission.ID")]),]
print(names(ebd_all))
#Calculate the encounters per species per range
ebd_density_division <- dcast(ebd_all, Taxonomic.Order +
English.India +
Scientific.Name +
IUCN +
WG +
Feeding.Guild +
Raptors +
Primary.Hole.nesters +
Woodland.Understorey.Birds +
Parasitic.Cuckoos ~ ., value.var = "Submission.ID", fun.aggregate = length )
# Dividing the encounters by number of lists for each range.
# Pick the generic columns out, only divide the columns with encounter and then cbind them back.
# Rounding to two decimal places
# Note, the data frame is transposed for division and transposed back
ebd_density_division <- cbind (ebd_density_division [1:10],
t( round (100 * t(ebd_density_division[11]) / nrow(ebd_complete_lists), 2)))
ebd_density_division <- ebd_density_division [ order (-ebd_density_division [11]),]
# Add a Serial Number
ebd_density_division$SlNo <- seq.int(nrow(ebd_density_division))
colnames (ebd_density_division) <- c ("Taxonomic.Order",
"English Name" ,
"Scientific Name",
"IUCN",
"WG",
"Feeding Guild",
"Raptors",
"Primary Hole Nesters",
"Woodland Understorey Birds",
"Parasitic Cuckoos",
"Density",
"SlNo")
return (ebd_density_division )
}
# Test Code
testHarness_generateBirdDensity <- function () {
# unzip('..\\data\\ebird_1489816770850.zip')
ebd <- read.csv('MyEBirdData.csv', header = TRUE, sep = ",")
species <- read.csv('Speciesv2.csv', header = TRUE, sep = ",")
# Obtain details of birds by joining with species file
ebd <- left_join (ebd, species, by = 'Scientific.Name')
ebd$RANGE <- 'Vazhachal'
ebd$RANGE [100:200] <- 'Sholayar'
ebd$RANGE [200:500] <- 'Charpa'
ebd$RANGE [500:800] <- 'Kollathirumedu'
output <- generateBirdDensity(ebd)
write.csv(output, 'testout.csv')
# output <- generateOverallBirdDensity(ebd)
write.csv(output, 'testout.csv')
print (nrow(output))
}
#testHarness_generateBirdDensity()