Import data

# excel file
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

Apply the following dplyr verbs to your data

Filter rows

# Filtering for rows with CO2 emissions over 100
filter(food, co2_emmission > 100)
## # A tibble: 286 × 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.
## # ℹ 276 more rows

Arrange rows

# Arranging data in descending order by co2 emissions, then by consumption
arrange(food, desc(co2_emmission), desc(consumption))
## # A tibble: 1,430 × 4
##    country          food_category consumption co2_emmission
##    <chr>            <chr>               <dbl>         <dbl>
##  1 Argentina        Beef                 55.5         1712 
##  2 Brazil           Beef                 39.2         1211.
##  3 USA              Beef                 36.2         1118.
##  4 Australia        Beef                 33.9         1045.
##  5 Bermuda          Beef                 33.2         1023.
##  6 French Polynesia Beef                 30.9          954.
##  7 Canada           Beef                 30.2          933.
##  8 Luxembourg       Beef                 29.9          922.
##  9 Uruguay          Beef                 29.1          898.
## 10 Israel           Beef                 28.8          888.
## # ℹ 1,420 more rows

Select columns

# Selecting only 3 of 4 columns; not interested in viewing countries
select(food, food_category, consumption, co2_emmission)
## # A tibble: 1,430 × 3
##    food_category consumption co2_emmission
##    <chr>               <dbl>         <dbl>
##  1 Beef                 55.5         1712 
##  2 Beef                 33.9         1045.
##  3 Beef                 22.5          694.
##  4 Beef                 13.4          412.
##  5 Beef                 22.5          694.
##  6 Beef                 36.2         1118.
##  7 Beef                 29.1          898.
##  8 Beef                 29.9          922.
##  9 Beef                 39.2         1211.
## 10 Beef                 23.4          721.
## # ℹ 1,420 more rows

Add columns

# Add column calculating difference between average co2 emmission and average consumption 
mutate(food, 
       gap_co2_consumption = co2_emmission - consumption)
## # A tibble: 1,430 × 5
##    country     food_category consumption co2_emmission gap_co2_consumption
##    <chr>       <chr>               <dbl>         <dbl>               <dbl>
##  1 Argentina   Beef                 55.5         1712                1657.
##  2 Australia   Beef                 33.9         1045.               1011.
##  3 Albania     Beef                 22.5          694.                672.
##  4 Iceland     Beef                 13.4          412.                399.
##  5 New Zealand Beef                 22.5          694.                672.
##  6 USA         Beef                 36.2         1118.               1082.
##  7 Uruguay     Beef                 29.1          898.                869.
##  8 Luxembourg  Beef                 29.9          922.                892.
##  9 Brazil      Beef                 39.2         1211.               1172.
## 10 Kazakhstan  Beef                 23.4          721.                698.
## # ℹ 1,420 more rows

Summarize by groups

food %>%
    
    # Group by food category
    group_by(food_category) %>%
    
    # Calculate average co2 emissions 
    summarise(avg_emmission = mean(co2_emmission)) %>%
    
    # Sort it
    arrange(avg_emmission)
## # A tibble: 11 × 2
##    food_category            avg_emmission
##    <chr>                            <dbl>
##  1 Soybeans                         0.387
##  2 Nuts inc. Peanut Butter          7.32 
##  3 Eggs                             7.50 
##  4 Wheat and Wheat Products        13.6  
##  5 Poultry                         22.8  
##  6 Fish                            27.6  
##  7 Rice                            37.6  
##  8 Pork                            57.1  
##  9 Lamb & Goat                     91.1  
## 10 Milk - inc. cheese             179.   
## 11 Beef                           374.

```