Import your data

# excel file
refugee <- read_excel("../00_data/mydata.xlsx") %>% janitor::clean_names()

data

Pivoting

long to wide form

refugee
## # A tibble: 64,809 × 16
##    the_year_of_data_collection country_of_origin_name     country_of_origin_un…¹
##                          <dbl> <chr>                      <chr>                 
##  1                        2010 Afghanistan                AFG                   
##  2                        2010 Iran (Islamic Rep. of)     IRN                   
##  3                        2010 Iraq                       IRQ                   
##  4                        2010 Pakistan                   PAK                   
##  5                        2010 Egypt                      ARE                   
##  6                        2010 China                      CHI                   
##  7                        2010 Palestinian                GAZ                   
##  8                        2010 Iraq                       IRQ                   
##  9                        2010 Serbia and Kosovo: S/RES/… SRB                   
## 10                        2010 Türkiye                    TUR                   
## # ℹ 64,799 more rows
## # ℹ abbreviated name: ¹​country_of_origin_unhcr_code
## # ℹ 13 more variables: country_of_origin_iso_code <chr>,
## #   country_of_asylum_name <chr>, country_of_asylum_unhcr_code <chr>,
## #   country_of_asylum_iso_code <chr>, the_number_of_refugees <dbl>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>, …
refugee_long <- refugee %>%
    
    pivot_longer(cols = c(`the_number_of_refugees`, `the_year_of_data_collection`), 
                 names_to = "year", 
                 values_to = "cases")

refugee_long
## # A tibble: 129,618 × 16
##    country_of_origin_name country_of_origin_unhcr_code country_of_origin_iso_c…¹
##    <chr>                  <chr>                        <chr>                    
##  1 Afghanistan            AFG                          AFG                      
##  2 Afghanistan            AFG                          AFG                      
##  3 Iran (Islamic Rep. of) IRN                          IRN                      
##  4 Iran (Islamic Rep. of) IRN                          IRN                      
##  5 Iraq                   IRQ                          IRQ                      
##  6 Iraq                   IRQ                          IRQ                      
##  7 Pakistan               PAK                          PAK                      
##  8 Pakistan               PAK                          PAK                      
##  9 Egypt                  ARE                          EGY                      
## 10 Egypt                  ARE                          EGY                      
## # ℹ 129,608 more rows
## # ℹ abbreviated name: ¹​country_of_origin_iso_code
## # ℹ 13 more variables: country_of_asylum_name <chr>,
## #   country_of_asylum_unhcr_code <chr>, country_of_asylum_iso_code <chr>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>,
## #   the_number_of_returne_internally_displaced_persons <dbl>, …

wide to long form

refugee_long %>%
    
    pivot_wider(names_from = "year", 
                values_from = "cases")
## Warning: Values from `cases` are not uniquely identified; output will contain list-cols.
## • Use `values_fn = list` to suppress this warning.
## • Use `values_fn = {summary_fun}` to summarise duplicates.
## • Use the following dplyr code to identify duplicates.
##   {data} %>%
##   dplyr::group_by(country_of_origin_name, country_of_origin_unhcr_code,
##   country_of_origin_iso_code, country_of_asylum_name,
##   country_of_asylum_unhcr_code, country_of_asylum_iso_code,
##   the_number_of_asylum_seekers, the_number_of_returned_refugees,
##   the_number_of_internally_displaced_persons,
##   the_number_of_returne_internally_displaced_persons,
##   the_number_of_stateless_persons, the_number_of_others_of_concern_to_unhcr,
##   the_number_of_other_people_in_need_of_international_protection,
##   the_numbero_of_host_community_members, year) %>%
##   dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
##   dplyr::filter(n > 1L)
## # A tibble: 45,196 × 16
##    country_of_origin_name          country_of_origin_un…¹ country_of_origin_is…²
##    <chr>                           <chr>                  <chr>                 
##  1 Afghanistan                     AFG                    AFG                   
##  2 Iran (Islamic Rep. of)          IRN                    IRN                   
##  3 Iraq                            IRQ                    IRQ                   
##  4 Pakistan                        PAK                    PAK                   
##  5 Egypt                           ARE                    EGY                   
##  6 China                           CHI                    CHN                   
##  7 Palestinian                     GAZ                    PSE                   
##  8 Iraq                            IRQ                    IRQ                   
##  9 Serbia and Kosovo: S/RES/1244 … SRB                    SRB                   
## 10 Türkiye                         TUR                    TUR                   
## # ℹ 45,186 more rows
## # ℹ abbreviated names: ¹​country_of_origin_unhcr_code,
## #   ²​country_of_origin_iso_code
## # ℹ 13 more variables: country_of_asylum_name <chr>,
## #   country_of_asylum_unhcr_code <chr>, country_of_asylum_iso_code <chr>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>, …

Separating and Uniting

Separate a column

refugee
## # A tibble: 64,809 × 16
##    the_year_of_data_collection country_of_origin_name     country_of_origin_un…¹
##                          <dbl> <chr>                      <chr>                 
##  1                        2010 Afghanistan                AFG                   
##  2                        2010 Iran (Islamic Rep. of)     IRN                   
##  3                        2010 Iraq                       IRQ                   
##  4                        2010 Pakistan                   PAK                   
##  5                        2010 Egypt                      ARE                   
##  6                        2010 China                      CHI                   
##  7                        2010 Palestinian                GAZ                   
##  8                        2010 Iraq                       IRQ                   
##  9                        2010 Serbia and Kosovo: S/RES/… SRB                   
## 10                        2010 Türkiye                    TUR                   
## # ℹ 64,799 more rows
## # ℹ abbreviated name: ¹​country_of_origin_unhcr_code
## # ℹ 13 more variables: country_of_origin_iso_code <chr>,
## #   country_of_asylum_name <chr>, country_of_asylum_unhcr_code <chr>,
## #   country_of_asylum_iso_code <chr>, the_number_of_refugees <dbl>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>, …
refugee_sep <- refugee %>%
    
    separate(col = the_year_of_data_collection, into = c("2010", "2011"))
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 64809 rows [1, 2, 3, 4,
## 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...].
refugee_sep
## # A tibble: 64,809 × 17
##    `2010` `2011` country_of_origin_name               country_of_origin_unhcr_…¹
##    <chr>  <chr>  <chr>                                <chr>                     
##  1 2010   <NA>   Afghanistan                          AFG                       
##  2 2010   <NA>   Iran (Islamic Rep. of)               IRN                       
##  3 2010   <NA>   Iraq                                 IRQ                       
##  4 2010   <NA>   Pakistan                             PAK                       
##  5 2010   <NA>   Egypt                                ARE                       
##  6 2010   <NA>   China                                CHI                       
##  7 2010   <NA>   Palestinian                          GAZ                       
##  8 2010   <NA>   Iraq                                 IRQ                       
##  9 2010   <NA>   Serbia and Kosovo: S/RES/1244 (1999) SRB                       
## 10 2010   <NA>   Türkiye                              TUR                       
## # ℹ 64,799 more rows
## # ℹ abbreviated name: ¹​country_of_origin_unhcr_code
## # ℹ 13 more variables: country_of_origin_iso_code <chr>,
## #   country_of_asylum_name <chr>, country_of_asylum_unhcr_code <chr>,
## #   country_of_asylum_iso_code <chr>, the_number_of_refugees <dbl>,
## #   the_number_of_asylum_seekers <dbl>, the_number_of_returned_refugees <dbl>,
## #   the_number_of_internally_displaced_persons <dbl>, …

Unite two columns

refugee_sep %>%
    
    unite(col = "the_number_of_refugees", c(the_number_of_asylum_seekers,the_number_of_internally_displaced_persons), sep = "/", )
## # A tibble: 64,809 × 15
##    `2010` `2011` country_of_origin_name               country_of_origin_unhcr_…¹
##    <chr>  <chr>  <chr>                                <chr>                     
##  1 2010   <NA>   Afghanistan                          AFG                       
##  2 2010   <NA>   Iran (Islamic Rep. of)               IRN                       
##  3 2010   <NA>   Iraq                                 IRQ                       
##  4 2010   <NA>   Pakistan                             PAK                       
##  5 2010   <NA>   Egypt                                ARE                       
##  6 2010   <NA>   China                                CHI                       
##  7 2010   <NA>   Palestinian                          GAZ                       
##  8 2010   <NA>   Iraq                                 IRQ                       
##  9 2010   <NA>   Serbia and Kosovo: S/RES/1244 (1999) SRB                       
## 10 2010   <NA>   Türkiye                              TUR                       
## # ℹ 64,799 more rows
## # ℹ abbreviated name: ¹​country_of_origin_unhcr_code
## # ℹ 11 more variables: country_of_origin_iso_code <chr>,
## #   country_of_asylum_name <chr>, country_of_asylum_unhcr_code <chr>,
## #   country_of_asylum_iso_code <chr>, the_number_of_refugees <chr>,
## #   the_number_of_returned_refugees <dbl>,
## #   the_number_of_returne_internally_displaced_persons <dbl>, …