Overview

Load the data into R, select useful nutritional variables, and transform the dataset into a simpler format for future analysis.

Dataset source: https://github.com/rfordatascience/tidytuesday/tree/main/data/2018/2018-09-04

library(tidyverse)

Loading the Data

The original dataset is loaded directly from its online source

fastfood <- read_csv(
  "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2018/2018-09-04/fastfood_calories.csv"
)
## New names:
## Rows: 515 Columns: 18
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (3): restaurant, item, salad dbl (15): ...1, calories, cal_fat, total_fat,
## sat_fat, trans_fat, cholestero...
## ℹ 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.
## • `` -> `...1`
head(fastfood)
## # A tibble: 6 × 18
##    ...1 restaurant item             calories cal_fat total_fat sat_fat trans_fat
##   <dbl> <chr>      <chr>               <dbl>   <dbl>     <dbl>   <dbl>     <dbl>
## 1     1 Mcdonalds  Artisan Grilled…      380      60         7       2       0  
## 2     2 Mcdonalds  Single Bacon Sm…      840     410        45      17       1.5
## 3     3 Mcdonalds  Double Bacon Sm…     1130     600        67      27       3  
## 4     4 Mcdonalds  Grilled Bacon S…      750     280        31      10       0.5
## 5     5 Mcdonalds  Crispy Bacon Sm…      920     410        45      12       0.5
## 6     6 Mcdonalds  Big Mac               540     250        28      10       1  
## # ℹ 10 more variables: cholesterol <dbl>, sodium <dbl>, total_carb <dbl>,
## #   fiber <dbl>, sugar <dbl>, protein <dbl>, vit_a <dbl>, vit_c <dbl>,
## #   calcium <dbl>, salad <chr>

Exploring Original Data

Inspected original data structure and column names.

dim(fastfood)
## [1] 515  18
names(fastfood)
##  [1] "...1"        "restaurant"  "item"        "calories"    "cal_fat"    
##  [6] "total_fat"   "sat_fat"     "trans_fat"   "cholesterol" "sodium"     
## [11] "total_carb"  "fiber"       "sugar"       "protein"     "vit_a"      
## [16] "vit_c"       "calcium"     "salad"
glimpse(fastfood)
## Rows: 515
## Columns: 18
## $ ...1        <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ restaurant  <chr> "Mcdonalds", "Mcdonalds", "Mcdonalds", "Mcdonalds", "Mcdon…
## $ item        <chr> "Artisan Grilled Chicken Sandwich", "Single Bacon Smokehou…
## $ calories    <dbl> 380, 840, 1130, 750, 920, 540, 300, 510, 430, 770, 380, 62…
## $ cal_fat     <dbl> 60, 410, 600, 280, 410, 250, 100, 210, 190, 400, 170, 300,…
## $ total_fat   <dbl> 7, 45, 67, 31, 45, 28, 12, 24, 21, 45, 18, 34, 20, 34, 8, …
## $ sat_fat     <dbl> 2.0, 17.0, 27.0, 10.0, 12.0, 10.0, 5.0, 4.0, 11.0, 21.0, 4…
## $ trans_fat   <dbl> 0.0, 1.5, 3.0, 0.5, 0.5, 1.0, 0.5, 0.0, 1.0, 2.5, 0.0, 1.5…
## $ cholesterol <dbl> 95, 130, 220, 155, 120, 80, 40, 65, 85, 175, 40, 95, 125, …
## $ sodium      <dbl> 1110, 1580, 1920, 1940, 1980, 950, 680, 1040, 1040, 1290, …
## $ total_carb  <dbl> 44, 62, 63, 62, 81, 46, 33, 49, 35, 42, 38, 48, 48, 67, 31…
## $ fiber       <dbl> 3, 2, 3, 2, 4, 3, 2, 3, 2, 3, 2, 3, 3, 5, 2, 2, 3, 3, 5, 2…
## $ sugar       <dbl> 11, 18, 18, 18, 18, 9, 7, 6, 7, 10, 5, 11, 11, 11, 6, 3, 1…
## $ protein     <dbl> 37, 46, 70, 55, 46, 25, 15, 25, 25, 51, 15, 32, 42, 33, 13…
## $ vit_a       <dbl> 4, 6, 10, 6, 6, 10, 10, 0, 20, 20, 2, 10, 10, 10, 2, 4, 6,…
## $ vit_c       <dbl> 20, 20, 20, 25, 20, 2, 2, 4, 4, 6, 0, 10, 20, 15, 2, 6, 15…
## $ calcium     <dbl> 20, 20, 50, 20, 20, 15, 10, 2, 15, 20, 15, 35, 35, 35, 4, …
## $ salad       <chr> "Other", "Other", "Other", "Other", "Other", "Other", "Oth…

Data Transformation

For this analysis, I selected variables related to the restaurant, menu item, calories, fat, sodium, carbohydrates, sugar, and protein. I also renamed several columns to make their meanings easier to understand.

fastfood_clean <- fastfood %>%
  select(
    restaurant,
    item,
    calories,
    total_fat,
    sodium,
    total_carb,
    sugar,
    protein
  ) %>%
  rename(
    menu_item = item,
    total_fat_grams = total_fat,
    sodium_mg = sodium,
    carbohydrates_grams = total_carb,
    sugar_grams = sugar,
    protein_grams = protein
  )

head(fastfood_clean)
## # A tibble: 6 × 8
##   restaurant menu_item    calories total_fat_grams sodium_mg carbohydrates_grams
##   <chr>      <chr>           <dbl>           <dbl>     <dbl>               <dbl>
## 1 Mcdonalds  Artisan Gri…      380               7      1110                  44
## 2 Mcdonalds  Single Baco…      840              45      1580                  62
## 3 Mcdonalds  Double Baco…     1130              67      1920                  63
## 4 Mcdonalds  Grilled Bac…      750              31      1940                  62
## 5 Mcdonalds  Crispy Baco…      920              45      1980                  81
## 6 Mcdonalds  Big Mac           540              28       950                  46
## # ℹ 2 more variables: sugar_grams <dbl>, protein_grams <dbl>

Cleaned Data

The resulting dataframe contains a smaller set of clearly labeled variables that are useful for comparing nutritional information across fast-food menu items.

dim(fastfood_clean)
## [1] 515   8
names(fastfood_clean)
## [1] "restaurant"          "menu_item"           "calories"           
## [4] "total_fat_grams"     "sodium_mg"           "carbohydrates_grams"
## [7] "sugar_grams"         "protein_grams"
head(fastfood_clean)
## # A tibble: 6 × 8
##   restaurant menu_item    calories total_fat_grams sodium_mg carbohydrates_grams
##   <chr>      <chr>           <dbl>           <dbl>     <dbl>               <dbl>
## 1 Mcdonalds  Artisan Gri…      380               7      1110                  44
## 2 Mcdonalds  Single Baco…      840              45      1580                  62
## 3 Mcdonalds  Double Baco…     1130              67      1920                  63
## 4 Mcdonalds  Grilled Bac…      750              31      1940                  62
## 5 Mcdonalds  Crispy Baco…      920              45      1980                  81
## 6 Mcdonalds  Big Mac           540              28       950                  46
## # ℹ 2 more variables: sugar_grams <dbl>, protein_grams <dbl>

Conclusions

The dataset was cleaned to focus on key nutritional variables such as calories, fat, sodium, sugar, and protein. Future analysis could compare restaurants more closely or use newer nutrition data to see how fast-food menus have changed over time.