This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.qmd
116 lines (105 loc) · 3.03 KB
/
index.qmd
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
---
title: "Trust in police"
author: "James Goldie"
format: html
---
```{r}
#| label: setup
library(tidyverse)
library(here)
```
```{r}
#| label: combine
open_and_lengthen <- function(path) {
read_csv(path,
col_types = cols(
occupation = col_character(),
year = col_integer(),
.default = col_double())) |>
pivot_longer(
-c(occupation, year),
names_to = c("measure", "country_code"), names_sep = "_",
values_to = "pct")
}
# open each file and bind together. (lengthen each csv before we combine in
# order to avoid blocks of nas)
list.files(
here("data"),
pattern = glob2rx("ipsos-trust-professions-bycountry-*.csv"),
full.names = TRUE) |>
map_dfr(open_and_lengthen) ->
trust_wide
```
```{r}
#| label: trust-vs-distrust
trust_wide |>
filter(country_code != "TOT") |>
pivot_wider(
id_cols = c("occupation", "year", "country_code"),
names_from = "measure",
values_from = "pct") ->
trust_distrust
```
```{r}
#| label: add-country-names
# map country codes to names (note some changes in coding year-to-year)
countries <- tribble(
~ country_code, ~ country_name,
"ARG", "Argentina",
"AUS", "Australia",
"BEL", "Belgium",
"BRA", "Brazil",
"CAN", "Canada",
"CHL", "Chile",
"CHN", "China",
"COL", "Colombia",
"GER", "Germany",
"DEU", "Germany",
"DNK", "Denmark",
"SPA", "Spain",
"ESP", "Spain",
"FRA", "France",
"GBR", "United Kingdom",
"GB", "United Kingdom",
"HUN", "Hungary",
"IND", "India",
"ITA", "Italy",
"JAP", "Japan",
"KOR", "South Korea",
"MEX", "Mexico",
"MYS", "Malaysia",
"NDL", "Netherlands",
"NLD", "Netherlands",
"PER", "Peru",
"POL", "Poland",
"KSA", "Saudi Arabia",
"SWE", "Sweden",
"TUR", "Turkey",
"USA", "United States",
"RSA", "South Africa",
"RUS", "Russia")
# join country names and write out to disk
trust_distrust |>
left_join(countries, by = "country_code") |>
mutate(neither = 100 - trust - distrust) |>
select(starts_with("country"), year, occupation, trust, neither, distrust) |>
write_csv(
here("data", "ipsos-trust-professions-bycountry.csv"), na = "") ->
trust_distrust_countrynames
trust_distrust_countrynames |>
filter(country_name == "Saudi Arabia", occupation == "The police")
```
# Police vs. other occupations
```{r}
trust_distrust_countrynames |>
mutate(occ_ispolice = occupation == "The police") |>
group_by(country_name, year, occ_ispolice) |>
summarise(across(c(trust, neither, distrust), mean)) |>
mutate(
occupation = if_else(occ_ispolice, "The police", "Other occupations")) |>
ungroup() |>
select(starts_with("country"), year, occupation, trust, neither, distrust) |>
write_csv(
here("data", "ipsos-trust-police-vs-others-bycountry.csv"), na = "") ->
trust_distrust_police
```