Import your data

food <- read_excel("data/my_Data_updated.xlsx")
food
## # A tibble: 1,430 × 4
##    country     food_category consumption co2_emmission
##    <chr>       <chr>               <dbl>         <dbl>
##  1 Argentina   Beef                 55.5         1712 
##  2 Australia   Beef                 33.9         1045.
##  3 Albania     Beef                 22.5          694.
##  4 Iceland     Beef                 13.4          412.
##  5 New Zealand Beef                 22.5          694.
##  6 USA         Beef                 36.2         1118.
##  7 Uruguay     Beef                 29.1          898.
##  8 Luxembourg  Beef                 29.9          922.
##  9 Brazil      Beef                 39.2         1211.
## 10 Kazakhstan  Beef                 23.4          721.
## # ℹ 1,420 more rows

Selecting smaller sample dataset

food_small <- food %>%
    select(food_category, consumption, co2_emmission) %>% 
    filter(food_category %in% c("Beef", "Poultry", "Rice")) %>% 
    sample_n(10)
food_small
## # A tibble: 10 × 3
##    food_category consumption co2_emmission
##    <chr>               <dbl>         <dbl>
##  1 Rice                 2.64          3.38
##  2 Rice                 7.78          9.96
##  3 Poultry              6.18          6.64
##  4 Rice                 5.74          7.35
##  5 Beef                 3.46        107.  
##  6 Poultry              4.62          4.96
##  7 Rice                85.2         109   
##  8 Beef                12.2         375.  
##  9 Rice                 2.56          3.28
## 10 Poultry              1.56          1.68

Pivoting

long to wide form

food_wide <- food_small %>% 
    pivot_wider(names_from = food_category, 
                values_from = consumption)
food_wide
## # A tibble: 10 × 4
##    co2_emmission  Rice Poultry  Beef
##            <dbl> <dbl>   <dbl> <dbl>
##  1          3.38  2.64   NA    NA   
##  2          9.96  7.78   NA    NA   
##  3          6.64 NA       6.18 NA   
##  4          7.35  5.74   NA    NA   
##  5        107.   NA      NA     3.46
##  6          4.96 NA       4.62 NA   
##  7        109    85.2    NA    NA   
##  8        375.   NA      NA    12.2 
##  9          3.28  2.56   NA    NA   
## 10          1.68 NA       1.56 NA

wide to long form

food_wide %>% 
    pivot_longer(cols = `Beef`:`Rice`,
                 names_to = "food_category", 
                 values_to = "consumption",
                 values_drop_na = TRUE)
## # A tibble: 10 × 3
##    co2_emmission food_category consumption
##            <dbl> <chr>               <dbl>
##  1          3.38 Rice                 2.64
##  2          9.96 Rice                 7.78
##  3          6.64 Poultry              6.18
##  4          7.35 Rice                 5.74
##  5        107.   Beef                 3.46
##  6          4.96 Poultry              4.62
##  7        109    Rice                85.2 
##  8        375.   Beef                12.2 
##  9          3.28 Rice                 2.56
## 10          1.68 Poultry              1.56

Separating and Uniting

Separate a column

food_small %>% 
    separate(co2_emmission, into = c("whole_number", "decimal", sep = 2))
## Warning: Expected 3 pieces. Missing pieces filled with `NA` in 10 rows [1, 2, 3, 4, 5,
## 6, 7, 8, 9, 10].
## # A tibble: 10 × 5
##    food_category consumption whole_number decimal `2`  
##    <chr>               <dbl> <chr>        <chr>   <chr>
##  1 Rice                 2.64 3            38      <NA> 
##  2 Rice                 7.78 9            96      <NA> 
##  3 Poultry              6.18 6            64      <NA> 
##  4 Rice                 5.74 7            35      <NA> 
##  5 Beef                 3.46 106          77      <NA> 
##  6 Poultry              4.62 4            96      <NA> 
##  7 Rice                85.2  109          <NA>    <NA> 
##  8 Beef                12.2  374          92      <NA> 
##  9 Rice                 2.56 3            28      <NA> 
## 10 Poultry              1.56 1            68      <NA>

Unite two columns

food_small %>% 
    
    unite(col = "cons_emmission", c(consumption, co2_emmission), sep = "/")
## # A tibble: 10 × 2
##    food_category cons_emmission
##    <chr>         <chr>         
##  1 Rice          2.64/3.38     
##  2 Rice          7.78/9.96     
##  3 Poultry       6.18/6.64     
##  4 Rice          5.74/7.35     
##  5 Beef          3.46/106.77   
##  6 Poultry       4.62/4.96     
##  7 Rice          85.18/109     
##  8 Beef          12.15/374.92  
##  9 Rice          2.56/3.28     
## 10 Poultry       1.56/1.68

Missing Values

food_small %>% 
    
    complete(consumption, food_category)
## # A tibble: 30 × 3
##    consumption food_category co2_emmission
##          <dbl> <chr>                 <dbl>
##  1        1.56 Beef                  NA   
##  2        1.56 Poultry                1.68
##  3        1.56 Rice                  NA   
##  4        2.56 Beef                  NA   
##  5        2.56 Poultry               NA   
##  6        2.56 Rice                   3.28
##  7        2.64 Beef                  NA   
##  8        2.64 Poultry               NA   
##  9        2.64 Rice                   3.38
## 10        3.46 Beef                 107.  
## # ℹ 20 more rows