Cleaning

1.Set Up

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

2.Import raw dataset

df <- read_csv("data/refugee_raw.csv")
Rows: 34600 Columns: 12
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): Country of origin, Country of asylum
dbl (10): Year, Refugees under UNHCR's mandate, Asylum-seekers, Returned ref...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(df)
Rows: 34,600
Columns: 12
$ Year                                               <dbl> 2019, 2019, 2019, 2…
$ `Country of origin`                                <chr> "Afghanistan", "Afg…
$ `Country of asylum`                                <chr> "Afghanistan", "Egy…
$ `Refugees under UNHCR's mandate`                   <dbl> 0, 28, 12, 5, 11585…
$ `Asylum-seekers`                                   <dbl> 0, 36, 0, 0, 1710, …
$ `Returned refugees`                                <dbl> 0, 0, 0, 0, 0, 0, 5…
$ `IDPs of concern to UNHCR`                         <dbl> 2553390, 0, 0, 0, 0…
$ `Returned IDPss`                                   <dbl> 0, 0, 0, 0, 0, 0, 0…
$ `Stateless persons`                                <dbl> 0, 0, 0, 0, 0, 0, 0…
$ `Others of concern`                                <dbl> 447093, 0, 0, 0, 0,…
$ `Other people in need of international protection` <dbl> NA, NA, NA, NA, NA,…
$ `Host Community`                                   <dbl> 0, 0, 0, 0, 0, 0, 0…

3. clean column names

df1 <- df |> 
  rename(year = Year,
         country_of_origin = `Country of origin`,
         country_of_asylum = `Country of asylum`,
         refugees_under_unhcrs_mandate = `Refugees under UNHCR's mandate`,
         asylum_seekers = `Asylum-seekers`,
         returned_refugees = `Returned refugees`,
         idps_of_concern_to_unhcr = `IDPs of concern to UNHCR`,
         returned_idps = `Returned IDPss`,
         stateless_persons = `Stateless persons`,
         others_of_concern = `Others of concern`,
         other_people_in_need_of_international_protection = `Other people in need of international protection`,
         host_community = `Host Community`)
glimpse(df1)
Rows: 34,600
Columns: 12
$ year                                             <dbl> 2019, 2019, 2019, 201…
$ country_of_origin                                <chr> "Afghanistan", "Afgha…
$ country_of_asylum                                <chr> "Afghanistan", "Egypt…
$ refugees_under_unhcrs_mandate                    <dbl> 0, 28, 12, 5, 11585, …
$ asylum_seekers                                   <dbl> 0, 36, 0, 0, 1710, 10…
$ returned_refugees                                <dbl> 0, 0, 0, 0, 0, 0, 5, …
$ idps_of_concern_to_unhcr                         <dbl> 2553390, 0, 0, 0, 0, …
$ returned_idps                                    <dbl> 0, 0, 0, 0, 0, 0, 0, …
$ stateless_persons                                <dbl> 0, 0, 0, 0, 0, 0, 0, …
$ others_of_concern                                <dbl> 447093, 0, 0, 0, 0, 0…
$ other_people_in_need_of_international_protection <dbl> NA, NA, NA, NA, NA, N…
$ host_community                                   <dbl> 0, 0, 0, 0, 0, 0, 0, …
df2 <- df1 |>
  filter(country_of_origin == "Afghanistan")

4. Save cleaned data

df_clean <- df2

save(df_clean, file = "data/refugee_cleaned.RData")

write_csv(df2, "data/df_clean.csv")