Data cleaning

Load Raw Data

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
df <- read_csv("undata.csv")
Rows: 100000 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Country or territory of asylum or residence, Country or territory o...
dbl (5): Year, Refugees*, Refugees assisted by UNHCR, Total refugees and peo...

ℹ 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.

Inspect data

# Check the format, rows and columns, and column names
glimpse(df)
Rows: 100,000
Columns: 7
$ `Country or territory of asylum or residence`                            <chr> …
$ `Country or territory of origin`                                         <chr> …
$ Year                                                                     <dbl> …
$ `Refugees*`                                                              <dbl> …
$ `Refugees assisted by UNHCR`                                             <dbl> …
$ `Total refugees and people in refugee-like situations**`                 <dbl> …
$ `Total refugees and people in refugee-like situations assisted by UNHCR` <dbl> …
# Check the first ten rows
head(df, 10)
# A tibble: 10 × 7
   Country or territory of asylum or …¹ Country or territory…²  Year `Refugees*`
   <chr>                                <chr>                  <dbl>       <dbl>
 1 Afghanistan                          Iran (Islamic Rep. of)  2024          39
 2 Afghanistan                          Pakistan                2024       20827
 3 Albania                              Afghanistan             2024           5
 4 Albania                              China                   2024          14
 5 Albania                              Egypt                   2024           9
 6 Albania                              Serbia and Kosovo: S/…  2024          45
 7 Albania                              State of Palestine      2024          13
 8 Albania                              Syrian Arab Rep.        2024          34
 9 Albania                              Ukraine                 2024          37
10 Albania                              Uzbekistan              2024           6
# ℹ abbreviated names: ¹​`Country or territory of asylum or residence`,
#   ²​`Country or territory of origin`
# ℹ 3 more variables: `Refugees assisted by UNHCR` <dbl>,
#   `Total refugees and people in refugee-like situations**` <dbl>,
#   `Total refugees and people in refugee-like situations assisted by UNHCR` <dbl>

Identify cleaning issues

# 1. The variable name is too long.
# 2. The character type of year is not Integer.

Data Cleaning

# Rename the column
df1 <- df |>
  rename(ACountry = `Country or territory of asylum or residence`,
         OCountry = `Country or territory of origin`,
         year = Year,
         refugees = `Refugees*`,
         UNassisted = `Refugees assisted by UNHCR`,
         total_refugees = `Total refugees and people in refugee-like situations**`,
         total_assisted = `Total refugees and people in refugee-like situations assisted by UNHCR`)
# Take a glimpse at the dataset
glimpse(df1)
Rows: 100,000
Columns: 7
$ ACountry       <chr> "Afghanistan", "Afghanistan", "Albania", "Albania", "Al…
$ OCountry       <chr> "Iran (Islamic Rep. of)", "Pakistan", "Afghanistan", "C…
$ year           <dbl> 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2…
$ refugees       <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 37, 6, 5, 16, 85, 95, …
$ UNassisted     <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 23, 6, 5, 16, 85, 95, …
$ total_refugees <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 9250, 6, 5, 16, 85, 95…
$ total_assisted <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 46, 6, 5, 16, 85, 95, …
# Change the character type of year
df_clean <- df1 |>
  mutate(year = as.integer(year))
# Take a glimpse at the dataset
glimpse(df_clean)
Rows: 100,000
Columns: 7
$ ACountry       <chr> "Afghanistan", "Afghanistan", "Albania", "Albania", "Al…
$ OCountry       <chr> "Iran (Islamic Rep. of)", "Pakistan", "Afghanistan", "C…
$ year           <int> 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2…
$ refugees       <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 37, 6, 5, 16, 85, 95, …
$ UNassisted     <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 23, 6, 5, 16, 85, 95, …
$ total_refugees <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 9250, 6, 5, 16, 85, 95…
$ total_assisted <dbl> 39, 20827, 5, 14, 9, 45, 13, 34, 46, 6, 5, 16, 85, 95, …

Save cleaned version to .RData

# Save the data in .RData since it preserves the format and is smaller in size
save(df_clean, file = "data_clean.RData")