-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-match-to-classifications.R
145 lines (133 loc) · 4.18 KB
/
02-match-to-classifications.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
library(tidyverse)
library(googlesheets4)
library(fuzzyjoin)
library(rvest)
library(glue)
gs4_auth()
participants <- read_csv(Sys.getenv("CSV_PATH"))
classifications_ca100plus <-
tibble(
affiliation_ca100plus =
map(seq(1, 7), function(page) {
read_html(str_c("https://www.climateaction100.org/whos-involved/companies/page/", page)) |>
html_nodes("h3.card-header-title") |>
html_text() |>
str_trim()
}) |>
unlist(),
ff_ca100plus = "Yes")
classifications_ngo <-
read_sheet(Sys.getenv("NGO_SHEET_ID"), sheet = 1) |>
select(
delegation_ngo = `Official Name`,
ff_delegation = `Count as FF lobbyists`,
ff_director = `Org with fossil fuel member(s) / FF rep on board of directors`,
ff_employees = `Count direct employees as FF lobbyists`) |>
filter(!is.na(delegation_ngo))
classifications_cop25 <-
read_sheet(Sys.getenv("COP25_SHEET_ID"), sheet = 1, skip = 1) |>
select(
affiliation_cop25 = `Affiliation original`,
ff_affiliation = `FF affiliation`) |>
filter(
!is.na(affiliation_cop25),
affiliation_cop25 != "NA")
matches_ca100plus <-
stringdist_inner_join(
x = participants |>
distinct(id, affiliation) |>
filter(!is.na(affiliation)),
y = classifications_ca100plus,
by = c("affiliation" = "affiliation_ca100plus"),
distance_col = "string_dist") |>
select(-affiliation) |>
group_by(id) |>
mutate(n_matches_ca100plus = n()) |>
ungroup() |>
filter(
(n_matches_ca100plus <= as.integer(Sys.getenv("MAX_MATCH_N")) &
nchar(affiliation_ca100plus) >= as.integer(Sys.getenv("MIN_MATCH_CHAR"))) |
string_dist == 0
) |>
select(-string_dist)
matches_ngo <-
stringdist_inner_join(
x = participants |>
distinct(id, delegation) |>
filter(!is.na(delegation)),
y = classifications_ngo,
by = c("delegation" = "delegation_ngo"),
distance_col = "string_dist") |>
select(-delegation) |>
group_by(id) |>
mutate(n_matches_ngo = n()) |>
ungroup() |>
filter(
(n_matches_ngo <= as.integer(Sys.getenv("MAX_MATCH_N")) &
nchar(delegation_ngo) >= as.integer(Sys.getenv("MIN_MATCH_CHAR"))) |
string_dist == 0
) |>
select(-string_dist)
matches_cop25 <-
stringdist_inner_join(
x = participants |>
distinct(id, affiliation) |>
filter(!is.na(affiliation)),
y = classifications_cop25,
by = c("affiliation" = "affiliation_cop25"),
distance_col = "string_dist") |>
select(-affiliation) |>
group_by(id) |>
mutate(n_matches_cop25 = n()) |>
ungroup() |>
filter(
(n_matches_cop25 <= as.integer(Sys.getenv("MAX_MATCH_N")) &
nchar(affiliation_cop25) >= as.integer(Sys.getenv("MIN_MATCH_CHAR"))) |
string_dist == 0
) |>
select(-string_dist)
participants_matched <- participants |>
left_join(matches_cop25, by = "id") |>
left_join(matches_ngo, by = "id") |>
left_join(matches_ca100plus, by = "id") |>
rowwise() |>
mutate(ff_any = if_else(
"Yes" %in% c(ff_affiliation, ff_delegation, ff_director, ff_employees, ff_ca100plus),
"Yes",
"No")) |>
ungroup()
# pipe separate duplicate values
participants_pipe_separated <- participants_matched |>
group_by(id) |>
summarise_all(function(x) {
if (length(unique(x)) == 1) return(first(x))
return(str_c(x, collapse = " | "))
})
# subset and rename columns
participants_renamed <- participants_pipe_separated |>
select(
ID = id,
Delegation = delegation,
Salutation = salutation,
Name = name,
Title = title,
Affiliation = affiliation,
`FF any` = ff_any,
`Affiliation (COP25)` = affiliation_cop25,
`FF affiliation` = ff_affiliation,
`COP25 matches (n)` = n_matches_cop25,
`Delegation (NGO)` = delegation_ngo,
`FF delegation` = ff_delegation,
`FF director` = ff_director,
`FF employees` = ff_employees,
`NGO matches (n)` = n_matches_ngo,
`Affiliation (CA100+)` = affiliation_ca100plus,
`FF CA100+` = ff_ca100plus,
`CA100+ matches (n)` = n_matches_ca100plus
) |>
rename_with(
~ str_c("Matched ", .x),
.cols = `FF any`:`CA100+ matches (n)`
)
# write to google sheets
write_sheet(participants_renamed, Sys.getenv("RESULTS_SHEET_ID"), sheet = Sys.getenv("RESULTS_SHEET_NAME"))