-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5_SummarizeResults_SpecialQueries.Rmd
306 lines (257 loc) · 10.9 KB
/
5_SummarizeResults_SpecialQueries.Rmd
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
title: "Impact Indicators: Special Queries"
author: "Cameryn Brock"
date: "4/21/2022"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
year <- "2022"
# read in data for 'other indicators' for all sites and overlaps
sites <- read_csv(paste0(
"results/FY", str_sub(year, start = 3), "_ImpactIndicators_Other_Sites.csv"))
overlaps <- readRDS(
paste0("results/FY", str_sub(year, start = 3), "_ImpactIndicators_Other_Overlaps.rds"))
# Define summarizing function
other_summarize <- function(df){
df %>%
summarize(across(
.cols = c(area_ha, rest_area, population,
tstor_woody, tstor_soil, tstor_total,
carbon_seq_potl),
sum, na.rm = TRUE),
.groups = "keep")
}
vars <- c(
"country",
"ci_divisio",
"ci_sls_2")
```
## 1. Southern Cross Area Under Restoration
* Reporting levels = country and scape; please also include totals by restoration type
* Restoration Type column must be selected (remove blanks and “not applicable”)
* Use the CI start date column - should not include anything before 2017 (prior to SC)
```{r, Southern Cross Area Under Restoration}
for (v in seq_along(vars)){
user_group_1 <- vars[v]
user_group_2 <- "restoratio"
user_groups <- c(user_group_1, user_group_2)
sites_summary <- sites %>%
# remove sites before 2017
filter(ci_start_d > as.Date('2017-01-01'),
restoratio != "Not Applicable") %>%
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup()
remove_ids <- sites %>%
filter(ci_start_d < as.Date('2017-01-01') | restoratio == "Not Applicable") %>%
pluck("ci_id") %>%
unique()
# summarize overlaps
overlaps_summary <- overlaps %>%
rowwise() %>%
mutate(remove = any(unlist(ci_id) %in% remove_ids)) %>%
ungroup() %>%
filter(!remove == TRUE) %>%
dplyr::select((!!as.symbol(user_group_1)), (!!as.symbol(user_group_2)),
area_ha, rest_area, population, tstor_woody,
tstor_soil, tstor_total, carbon_seq_potl) %>%
rowwise() %>%
# get a list of duplicated values
mutate(duplicates = list((!!as.symbol(user_group_2))[duplicated((!!as.symbol(user_group_2)))])) %>%
ungroup() %>%
# remove those with no duplicates
filter(!duplicates == "character(0)") %>%
# multiply values of interest by the number of times its duplicated & make negative
rowwise() %>%
mutate({{ user_group_2 }} := list(unique(duplicates))) %>%
unnest({{ user_group_2 }}) %>%
ungroup() %>%
rowwise() %>%
mutate(duplicate_n = sum((!!as.symbol(user_group_2)) == duplicates, na.rm = TRUE)) %>%
mutate(across(
.cols = c(area_ha, rest_area, population, tstor_woody,
tstor_soil, tstor_total, carbon_seq_potl),
~ .x * -duplicate_n)) %>%
# do again for second grouping if needed
mutate(duplicates = list((!!as.symbol(user_group_1))[duplicated((!!as.symbol(user_group_1)))])) %>%
ungroup() %>%
filter(!duplicates == "character(0)") %>%
rowwise() %>%
mutate({{ user_group_1 }} := unique(duplicates)) %>%
# unnest({{ user_group_1 }}) %>%
ungroup() %>%
# summarize as you did with sites
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup()
# combine the two data frames and summarize to subtract the overlaps
corrected_summary <- sites_summary %>%
bind_rows(overlaps_summary) %>%
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup() %>%
dplyr::select(c(1,2,4))
write_csv(
corrected_summary,
file = paste0("results/summaries_", year, "/special_queries/ImpactIndicators", "_", str_to_title(user_group_1),
"_", str_to_title(user_group_2), "_restAreaPost2017.csv")
)
}
```
## 2. Southern Cross Potential Sequestration
* Reporting levels = country and scape; please also include totals by restoration type
* Restoration Type column must be selected (remove blanks and “not applicable”)
* If restoration type = Agroforestry, Enrichment Planting/Assisted Natural Regeneration, Mangrove Shrub Restoration, Mangrove Tree Restoration, Plantations & Woodlots – Mixed 50/50, Seed Dispersal, Wetland Restoration - Use the tree planting date – should not include anything prior to 2018 or after June 30, 2021
* If restoration type = Rangeland Restoration, natural regeneration - Use the CI start date - should not include anything prior to 2018 or after June 30, 2021
```{r, Southern Cross Potential Sequestration}
for (v in seq_along(vars)){
user_group_1 <- vars[v]
user_group_2 <- "restoratio"
user_groups <- c(user_group_1, user_group_2)
sites_summary <- sites %>%
# filter(restoratio != "Not Applicable") %>%
# # remove sites as requested
# mutate(remove = case_when(
# tree_plant > as.Date('2018-01-01') &
# tree_plant < as.Date('2021-06-30') ~ "keep",
# restoratio %in% c("Rangeland Restoration - Planned Grazing", "Natural Regeneration") &
# ci_start_d > as.Date('2018-01-01') &
# ci_start_d < as.Date('2021-06-30') ~ "keep",
# T ~ "remove"
# )) %>%
# filter(remove == "keep") %>%
# dplyr::select(!remove) %>%
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup()
remove_ids <- sites %>%
# remove sites as requested
mutate(remove = case_when(
tree_plant > as.Date('2018-01-01') &
tree_plant < as.Date('2021-06-30') ~ "keep",
restoratio %in% c("Rangeland Restoration - Planned Grazing", "Natural Regeneration") &
ci_start_d > as.Date('2018-01-01') &
ci_start_d < as.Date('2021-06-30') ~ "keep",
T ~ "remove"
)) %>%
filter(remove == "remove") %>%
pluck("ci_id") %>%
unique()
# summarize overlaps
overlaps_summary <- overlaps %>%
rowwise() %>%
mutate(remove = any(unlist(ci_id) %in% remove_ids)) %>%
ungroup() %>%
filter(!remove == TRUE) %>%
dplyr::select((!!as.symbol(user_group_1)), (!!as.symbol(user_group_2)),
area_ha, rest_area, population, tstor_woody,
tstor_soil, tstor_total, carbon_seq_potl) %>%
rowwise() %>%
# get a list of duplicated values
mutate(duplicates = list((!!as.symbol(user_group_2))[duplicated((!!as.symbol(user_group_2)))])) %>%
ungroup() %>%
# remove those with no duplicates
filter(!duplicates == "character(0)") %>%
# multiply values of interest by the number of times its duplicated & make negative
rowwise() %>%
mutate({{ user_group_2 }} := list(unique(duplicates))) %>%
unnest({{ user_group_2 }}) %>%
ungroup() %>%
rowwise() %>%
mutate(duplicate_n = sum((!!as.symbol(user_group_2)) == duplicates, na.rm = TRUE)) %>%
mutate(across(
.cols = c(area_ha, rest_area, population, tstor_woody,
tstor_soil, tstor_total, carbon_seq_potl),
~ .x * -duplicate_n)) %>%
# do again for second grouping if needed
mutate(duplicates = list((!!as.symbol(user_group_1))[duplicated((!!as.symbol(user_group_1)))])) %>%
ungroup() %>%
filter(!duplicates == "character(0)") %>%
rowwise() %>%
mutate({{ user_group_1 }} := unique(duplicates)) %>%
# unnest({{ user_group_1 }}) %>%
ungroup() %>%
# summarize as you did with sites
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup() %>%
filter(!restoratio == "Not Applicable")
# combine the two data frames and summarize to subtract the overlaps
corrected_summary <- sites_summary %>%
bind_rows(overlaps_summary) %>%
group_by_at(user_groups) %>%
other_summarize() %>%
ungroup() %>%
dplyr::select(c(1,2,9))
write_csv(
corrected_summary,
file = paste0("results/summaries_", year, "/special_queries/ImpactIndicators", "_", str_to_title(user_group_1),
"_", str_to_title(user_group_2), "_carbonSeqPost2018.csv")
)
}
```
## 3. Southern Cross Marine Protected Area
* Reporting levels = by country and scape (CI_SLS_2 column)
* Query "Total Marine Protected Area" (square kilometer)
+ Biome Column = Marine; Terrestrial + Marine
+ Intervention Type= Protected Area (Note: please do not include Protected Area – Proposed)
+ Gazettement Date= Should fall within FY21 (July 1 2020 – June 30 2021) OR
+ OR Improved Management Column = Include all options with “yes” in the selection (ex. “Yes, staff increased”); Remove - Blanks and Not Applicable
+ CI Division - Do not include anything tagged as “C4O - Blue Nature Alliance”
* New Marine Protected Area
+ Biome Column = Marine; Terrestrial + Marine
+ Intervention Type= Protected Area (Note: please do not include Protected Area – Proposed)
+ Gazettement Date= Should fall within FY21 (July 1 2020 – June 30 2021)
+ CI Division - Do not include anything tagged as “C4O - Blue Nature Alliance”
* Marine Protected Area with Improved Management Activities
+ Biome Column = Marine; Terrestrial + Marine
+ Intervention Type = Protected Area (Note: please do not include Protected Area – Proposed)
+ Improved Management Column = Include all options with “yes” in the selection (ex. “Yes, staff increased”); Remove - Blanks and Not Applicable
+ CI Division - Do not include anything tagged as “C4O - Blue Nature Alliance”
```{r, Southern Cross Marine Protected Area}
for (v in seq_along(vars)){
user_group <- vars[v]
sites_summary <- sites %>%
filter(
biome %in% c("Marine", "Terrestrial + Marine"),
interventi == "Protected Area (National or Regional)",
gazettemen > as.Date("2020-07-01") &
gazettemen < as.Date("2021-06-30") |
str_detect(improved_m, "Yes")) %>%
filter(is.na(ci_divis_1) | ci_divis_1 != "C4O - Blue Nature Alliance") %>%
group_by_at(user_group) %>%
summarize(
area_ha = sum(area_ha)) %>%
ungroup()
remove_ids <- sites %>%
filter(
!biome %in% c("Marine", "Terrestrial + Marine") |
interventi == "Protected Area (National or Regional)" |
(is.na(ci_divis_1) | ci_divis_1 != "C4O - Blue Nature Alliance") |
(gazettemen > as.Date("2020-07-01") &
gazettemen < as.Date("2021-06-30") |
str_detect(improved_m, "Yes"))) %>%
pluck("ci_id") %>%
unique()
# summarize overlaps
overlaps_summary <- overlaps %>%
rowwise() %>%
mutate(remove = any(unlist(ci_id) %in% remove_ids)) %>%
ungroup() %>%
filter(!remove == TRUE)
# 0 overlaps - don't need to correct anything from sites_summary
# may have to adjust in future years if there are overlaps
corrected_summary <- sites_summary
write_csv(
corrected_summary,
file = paste0("results/summaries_", year, "/special_queries/ImpactIndicators", "_", str_to_title(user_group),
"_marineProtectedAreas.csv")
)
}
```