淺談麥當勞食品的營養價值
分析過程:
第一步:理解數據
從kaggle下載數據,食品種類較多,不過數據相對來說不算很多。
數據有:Category、Item、Serving Size、Calories、Calories from Fat、Total Fat、Total Fat (% Daily Value)、Saturated Fat、Saturated Fat (% Daily Value)、Trans Fat、Cholesterol、Cholesterol (% Daily Value)、Sodium、Sodium (% Daily Value)、Carbohydrates、Carbohydrates (% Daily Value)、Dietary Fiber、Dietary Fiber (% Daily Value)、Sugars、Protein、Vitamin A (% Daily Value)、Vitamin C (% Daily Value)、Calcium (% Daily Value)、Iron (% Daily Value)。
分析要求是:
1、麥當勞食品的平均卡路里是多少?
2、飲用多少飲料,例如蘇打水或者咖啡,會加強總熱量的攝入?
3、購買烤雞肉三明治,而不是炸雞肉三明治,是否會更加有營養?
4、怎麼從菜單中選擇最少數量的食品來你滿足一天的營養需求?
第一條可以直接從食品的卡里路入手,第二條是計算不喝飲料和喝飲料的熱量有什麼區別,第三條是計算這兩種三明治的卡路里的差值,第四條同第三條,第五條是根據自己來做選擇。第二步:數據導入
第三步:數據預處理&數據計算1、麥當勞食品的平均卡路里是多少?
1.1選擇子集,只選擇Category,Item,Calories這三列數據。
> ##1.1選擇子集n> library(dplyr)nnAttaching package: 『dplyr』nnThe following objects are masked from 『package:stats』:nn filter, lagnnThe following objects are masked from 『package:base』:nn intersect, setdiff, setequal, unionnn> menu1 <- select(menu,Category,Item,Calories)n> menu1n# A tibble: 260 × 3n Category Item Caloriesn <chr> <chr> <int>n1 Breakfast Egg McMuffin 300n2 Breakfast Egg White Delight 250n3 Breakfast Sausage McMuffin 370n4 Breakfast Sausage McMuffin with Egg 450n5 Breakfast Sausage McMuffin with Egg Whites 400n6 Breakfast Steak & Egg McMuffin 430n7 Breakfast Bacon, Egg & Cheese Biscuit (Regular Biscuit) 460n8 Breakfast Bacon, Egg & Cheese Biscuit (Large Biscuit) 520n9 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Regular Biscuit) 410n10 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Large Biscuit) 470n# ... with 250 more rowsn> n
1.2查找卡路里數據是否有缺失值,刪除缺失值。
由上面一個代碼可以看出,目前共有250行數據,現在進行缺失值的刪除:
> ##1.2查找卡路里數據是否有缺失值,刪除缺失值n> menu1 <- filter(menu1,!is.na(Calories))n> menu1n# A tibble: 260 × 3n Category Item Caloriesn <chr> <chr> <int>n1 Breakfast Egg McMuffin 300n2 Breakfast Egg White Delight 250n3 Breakfast Sausage McMuffin 370n4 Breakfast Sausage McMuffin with Egg 450n5 Breakfast Sausage McMuffin with Egg Whites 400n6 Breakfast Steak & Egg McMuffin 430n7 Breakfast Bacon, Egg & Cheese Biscuit (Regular Biscuit) 460n8 Breakfast Bacon, Egg & Cheese Biscuit (Large Biscuit) 520n9 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Regular Biscuit) 410n10 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Large Biscuit) 470n# ... with 250 more rowsn> n
查找缺失值後,仍然有250行數據,也就是不存在缺失值。
1.3數據計算
由於要計算菜單的卡路里,所以將數據按照菜單種類進行分組:
> ##1.3按種類分組n> by_category <- group_by(menu1,Category)n> by_categorynSource: local data frame [260 x 3]nGroups: Category [9] nn Category Item Caloriesn <chr> <chr> <int>n1 Breakfast Egg McMuffin 300n2 Breakfast Egg White Delight 250n3 Breakfast Sausage McMuffin 370n4 Breakfast Sausage McMuffin with Egg 450n5 Breakfast Sausage McMuffin with Egg Whites 400n6 Breakfast Steak & Egg McMuffin 430n7 Breakfast Bacon, Egg & Cheese Biscuit (Regular Biscuit) 460n8 Breakfast Bacon, Egg & Cheese Biscuit (Large Biscuit) 520n9 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Regular Biscuit) 410n10 Breakfast Bacon, Egg & Cheese Biscuit with Egg Whites (Large Biscuit) 470n# ... with 250 more rowsn> n
從上面代碼可以得出,按照種類共分成9組,分別是Breakfast、Beef & Pork、Desserts、Beverages、Coffee & Tea、Smoothies & Shakes、Chicken & Fish、Salads、Snacks & Sides。
接下來計算每個種類的卡路里平均值,以及按照卡路里的值做降序排序:
> ##1.4計算每一組的平均卡路里,和每一組的數量n> meancalories <- summarise(by_category,n+ count = n(),n+ mean_calories=mean(Calories,na.rm = T))n> meancaloriesn# A tibble: 9 × 3n Category count mean_caloriesn <chr> <int> <dbl>n1 Beef & Pork 15 494.0000n2 Beverages 27 113.7037n3 Breakfast 42 526.6667n4 Chicken & Fish 27 552.9630n5 Coffee & Tea 95 283.8947n6 Desserts 7 222.1429n7 Salads 6 270.0000n8 Smoothies & Shakes 28 531.4286n9 Snacks & Sides 13 245.7692n> ##將mean_calories進行排序n> meancalories <- arrange(meancalories,desc(mean_calories))n> meancaloriesn# A tibble: 9 × 3n Category count mean_caloriesn <chr> <int> <dbl>n1 Chicken & Fish 27 552.9630n2 Smoothies & Shakes 28 531.4286n3 Breakfast 42 526.6667n4 Beef & Pork 15 494.0000n5 Coffee & Tea 95 283.8947n6 Salads 6 270.0000n7 Snacks & Sides 13 245.7692n8 Desserts 7 222.1429n9 Beverages 27 113.7037n> n
其中Chicken & Fish卡路里值最高,其次是Smoothies & Shakes和Breakfast,最低的是Beverages。
為了方便查看,將上述數據做成了圖表形式:
> ##1.5數據顯示n> ##ggplot繪圖n> library(ggplot2)n> ggplot(data=meancalories,aes(x=(Category),y=mean_calories))+geom_bar(stat="identity",color="skyblue",fill="skyblue")+labs(title="The Calories rate of McDonalds Menu")n> n
從圖中也可以得出剛才的結論,Chicken & Fish卡路里值最高,其次是Smoothies & Shakes和Breakfast。所以,在麥當勞中Chicken & Fish類的食品需要少吃。
2、飲用多少飲料,例如蘇打水或者咖啡,會加強總熱量的攝入?
2.1選擇子集
在這個問題上,需要考慮Beverages對熱量攝入的影響,所以在上面的子集中將Beverages的種類單獨提取出來。
> ##2.1選擇子集,選擇Beverages類的數據。n> Beverages_caloric <- menu1[menu1$Category == "Beverages",]n> Beverages_caloricn# A tibble: 27 × 3n Category Item Caloriesn <chr> <chr> <int>n1 Beverages Coca-ColaClassic (Small) 140n2 Beverages Coca-ColaClassic (Medium) 200n3 Beverages Coca-ColaClassic (Large) 280n4 Beverages Coca-ColaClassic (Child) 100n5 Beverages DietCoke (Small) 0n6 Beverages DietCoke (Medium) 0n7 Beverages DietCoke (Large) 0n8 Beverages DietCoke (Child) 0n9 Beverages DrPepper (Small) 140n10 Beverages DrPepper (Medium) 190n# ... with 17 more rowsn> n
一共有17行的數據。
因為數據中飲料的分量有大份、中份、小份、兒童等,這些不需要考慮,所以將數據中的Item分割成兩列,再提取第一列的數據,然後進行分組計算平均卡路里:
> ##然後對Item進行分割成兩列。n> library(stringr)n> Item <- str_split_fixed(Beverages_caloric$Item," ",n=2)n> Itemn [,1] [,2] n [1,] "Coca-ColaClassic" " (Small)" n [2,] "Coca-ColaClassic" " (Medium)"n [3,] "Coca-ColaClassic" " (Large)" n [4,] "Coca-ColaClassic" " (Child)" n [5,] "DietCoke" " (Small)" n [6,] "DietCoke" " (Medium)"n [7,] "DietCoke" " (Large)" n [8,] "DietCoke" " (Child)" n [9,] "DrPepper" " (Small)" n[10,] "DrPepper" " (Medium)"n[11,] "DrPepper" " (Large)" n[12,] "DrPepper" " (Child)" n[13,] "DietDrPepper" " (Small)" n[14,] "DietDrPepper" " (Medium)"n[15,] "DietDrPepper" " (Large)" n[16,] "DietDrPepper" " (Child)" n[17,] "Sprite" " (Small)" n[18,] "Sprite" " (Medium)"n[19,] "Sprite" " (Large)" n[20,] "Sprite" " (Child)" n[21,] "1%LowFatMilkJug" "" n[22,] "FatFreeChocolateMilkJug" "" n[23,] "MinuteMaid100%AppleJuiceBox" "" n[24,] "MinuteMaidOrangeJuice" " (Small)" n[25,] "MinuteMaidOrangeJuice" " (Medium)"n[26,] "MinuteMaidOrangeJuice" " (Large)" n[27,] "DasaniWaterBottle" "" n> Beverages_caloric$Item <- Item[,1]n> Beverages_caloricn# A tibble: 27 × 3n Category Item Caloriesn <chr> <chr> <int>n1 Beverages Coca-ColaClassic 140n2 Beverages Coca-ColaClassic 200n3 Beverages Coca-ColaClassic 280n4 Beverages Coca-ColaClassic 100n5 Beverages DietCoke 0n6 Beverages DietCoke 0n7 Beverages DietCoke 0n8 Beverages DietCoke 0n9 Beverages DrPepper 140n10 Beverages DrPepper 190n# ... with 17 more rowsn> ##2.2數據計算n> ##按Item分組n> by_Item <- group_by(Beverages_caloric,Item)n> by_ItemnSource: local data frame [27 x 3]nGroups: Item [10]nn Category Item Caloriesn <chr> <chr> <int>n1 Beverages Coca-ColaClassic 140n2 Beverages Coca-ColaClassic 200n3 Beverages Coca-ColaClassic 280n4 Beverages Coca-ColaClassic 100n5 Beverages DietCoke 0n6 Beverages DietCoke 0n7 Beverages DietCoke 0n8 Beverages DietCoke 0n9 Beverages DrPepper 140n10 Beverages DrPepper 190n# ... with 17 more rowsn> ##2.3計算每一組的平均卡里路,和每一組的數量n> Item_meancalories <- summarise(by_Item,n+ count = n(),n+ mean_calories=mean(Calories,na.rm = T))n> Item_meancaloriesn# A tibble: 10 × 3n Item count mean_caloriesn <chr> <int> <dbl>n1 1%LowFatMilkJug 1 100.0000n2 Coca-ColaClassic 4 180.0000n3 DasaniWaterBottle 1 0.0000n4 DietCoke 4 0.0000n5 DietDrPepper 4 0.0000n6 DrPepper 4 175.0000n7 FatFreeChocolateMilkJug 1 130.0000n8 MinuteMaid100%AppleJuiceBox 1 80.0000n9 MinuteMaidOrangeJuice 3 206.6667n10 Sprite 4 180.0000n> ##將Item_meancalories進行排序n> Item_meancalories <- arrange(Item_meancalories,desc(mean_calories))n> Item_meancaloriesn# A tibble: 10 × 3n Item count mean_caloriesn <chr> <int> <dbl>n1 MinuteMaidOrangeJuice 3 206.6667n2 Coca-ColaClassic 4 180.0000n3 Sprite 4 180.0000n4 DrPepper 4 175.0000n5 FatFreeChocolateMilkJug 1 130.0000n6 1%LowFatMilkJug 1 100.0000n7 MinuteMaid100%AppleJuiceBox 1 80.0000n8 DasaniWaterBottle 1 0.0000n9 DietCoke 4 0.0000n10 DietDrPepper 4 0.0000n> ##2.3ggplot繪圖n> library(ggplot2)n> ggplot(data=Item_meancalories,aes(x=(Item),y=mean_calories))+geom_bar(stat="identity",color="lightpink",fill="lightpink")+labs(title="The Calories of McDonalds Beverages")n>n
到了這裡,基本上就可以知道Da sani Water Bottle、Diet Coke、Diet Dr Pepper這三種飲料對於卡路里熱量的吸收沒有影響,而Minute Maid Orange Juice的卡路里熱量最高,也就是說如果飲用Minute Maid Orange Juice飲料會增加卡路里的攝入。
3、購買烤雞肉三明治,而不是炸雞肉三明治,是否會更加有營養?
這個問題需要比較的是烤雞肉三明治和炸雞肉三明治,所以選擇Chicken & Fish種類的數據,然後再查看數據後選擇「Sandwich」的數據,而各類營養指標選擇「Category、Item、Calories、Cholesterol (% Daily Value)、Carbohydrates (% Daily Value)、Dietary Fiber (% Daily Value)、Sugars 、Protein 、Vitamin A (% Daily Value)、Vitamin C (% Daily Value)、Calcium (% Daily # Value)、Iron (% Daily Value)」:
> ##3.1選擇子集,選擇含 Grilled Chicken Sandwich和Crispy Chicken Sandwich類的數據。n> menu2 <- menu[menu$Category == "Chicken & Fish",]n> menu2n# A tibble: 27 × 24n Category Item `Serving Size` Calories `Calories from Fat` `Total Fat`n <chr> <chr> <chr> <int> <int> <dbl>n1 Chicken & Fish Premium Crispy Chicken Classic Sandwich 7.5 oz (213 g) 510 200 22n2 Chicken & Fish Premium Grilled Chicken Classic Sandwich 7 oz (200 g) 350 80 9n3 Chicken & Fish Premium Crispy Chicken Club Sandwich 8.8 oz (249 g) 670 300 33n4 Chicken & Fish Premium Grilled Chicken Club Sandwich 8.3 oz (235 g) 510 180 20n5 Chicken & Fish Premium Crispy Chicken Ranch BLT Sandwich 8.1 oz (230 g) 610 250 28n6 Chicken & Fish Premium Grilled Chicken Ranch BLT Sandwich 7.6 oz (217 g) 450 130 15n7 Chicken & Fish Bacon Clubhouse Crispy Chicken Sandwich 10 oz (284 g) 750 340 38n8 Chicken & Fish Bacon Clubhouse Grilled Chicken Sandwich 9.5 oz (270 g) 590 230 25n9 Chicken & Fish Southern Style Crispy Chicken Sandwich 5.6 oz (160 g) 430 170 19n10 Chicken & Fish McChicken 5.1 oz (143 g) 360 140 16n# ... with 17 more rows, and 18 more variables: `Total Fat (% Daily Value)` <int>, `Saturated Fat` <dbl>, `Saturated Fat (% Dailyn# Value)` <int>, `Trans Fat` <dbl>, Cholesterol <int>, `Cholesterol (% Daily Value)` <int>, Sodium <int>, `Sodium (% Daily Value)` <int>,n# Carbohydrates <int>, `Carbohydrates (% Daily Value)` <int>, `Dietary Fiber` <int>, `Dietary Fiber (% Daily Value)` <int>, Sugars <int>,n# Protein <int>, `Vitamin A (% Daily Value)` <int>, `Vitamin C (% Daily Value)` <int>, `Calcium (% Daily Value)` <int>, `Iron (% Dailyn# Value)` <int>n> View(menu2) ##前9行包含「sandwich」n> menu2 <- menu2[1:9,]n> menu2 <- menu2[,-3]n> menu2 <- menu2[,-4]n> menu2 <- menu2[,-4]n> menu2 <- menu2[,-4]n> menu2 <- menu2[,-6]n> menu2 <- menu2[,-6]n> menu2 <- menu2[,-(7:9)]n> menu2 <- menu2[,-8]n> menu2n# A tibble: 9 × 14n Category Item Calories `Saturated Fat` `Saturated Fat (% Daily Value)`n <chr> <chr> <int> <dbl> <int>n1 Chicken & Fish Premium Crispy Chicken Classic Sandwich 510 3.5 18n2 Chicken & Fish Premium Grilled Chicken Classic Sandwich 350 2.0 9n3 Chicken & Fish Premium Crispy Chicken Club Sandwich 670 9.0 44n4 Chicken & Fish Premium Grilled Chicken Club Sandwich 510 7.0 36n5 Chicken & Fish Premium Crispy Chicken Ranch BLT Sandwich 610 6.0 31n6 Chicken & Fish Premium Grilled Chicken Ranch BLT Sandwich 450 4.5 22n7 Chicken & Fish Bacon Clubhouse Crispy Chicken Sandwich 750 10.0 51n8 Chicken & Fish Bacon Clubhouse Grilled Chicken Sandwich 590 8.0 42n9 Chicken & Fish Southern Style Crispy Chicken Sandwich 430 3.0 15n# ... with 9 more variables: `Cholesterol (% Daily Value)` <int>, `Carbohydrates (% Daily Value)` <int>, `Dietary Fiber (% Dailyn# Value)` <int>, Sugars <int>, Protein <int>, `Vitamin A (% Daily Value)` <int>, `Vitamin C (% Daily Value)` <int>, `Calcium (% Dailyn# Value)` <int>, `Iron (% Daily Value)` <int>n> n
計算同種類的Grilled Chicken Sandwich和Crispy Chicken Sandwich個營養指標的差值,其中Crispy Chicken Sandwich有Premium Crispy Chicken Classic Sandwich、Premium Crispy Chicken Club Sandwich、Premium Crispy Chicken Ranch BLT Sandwich、Bacon Clubhouse Crispy Chicken Sandwich、Southern Style Crispy Chicken Sandwich這五種類別,Crispy Chicken Sandwich有Premium Grilled Chicken Classic Sandwich、Premium Grilled Chicken Club Sandwich、Premium Grilled Chicken Ranch BLT Sandwich、Bacon Clubhouse Grilled Chicken Sandwich這四種類別,由於Southern Style Crispy Chicken Sandwich這個類別Crispy Chicken Sandwich不存在,所以在這裡不考慮將這個類別進行對比,主要是對同一類別不同的Grilled Chicken Sandwich和Crispy Chicken Sandwich進行對比:
> ##3.2數據計算,計算同種類的Grilled Chicken Sandwich和Crispy Chicken Sandwich 各營養指標的差值n> classic_menu <- menu2[1:2,-(1:2)]n> club_menu <- menu2[3:4,-(1:2)]n> BLT_menu <- menu2[5:6,-(1:2)]n> Bacon_menu <- menu2[7:8,-(1:2)]n> ##計算n> value_classic <- apply(classic_menu,2,diff)n> data.frame(value_classic)n value_classicnCalories -160.0nSaturated Fat -1.5nSaturated Fat (% Daily Value) -9.0nCholesterol (% Daily Value) 6.0nCarbohydrates (% Daily Value) -4.0nDietary Fiber (% Daily Value) 0.0nSugars -2.0nProtein 4.0nVitamin A (% Daily Value) 0.0nVitamin C (% Daily Value) 2.0nCalcium (% Daily Value) 0.0nIron (% Daily Value) 0.0n> value_club <- apply(club_menu,2,diff)n> data.frame(value_club)n value_clubnCalories -160nSaturated Fat -2nSaturated Fat (% Daily Value) -8nCholesterol (% Daily Value) 6nCarbohydrates (% Daily Value) -4nDietary Fiber (% Daily Value) -1nSugars -2nProtein 4nVitamin A (% Daily Value) 0nVitamin C (% Daily Value) 0nCalcium (% Daily Value) 0nIron (% Daily Value) 0n> value_BLT <- apply(BLT_menu,2,diff)n> data.frame(value_BLT)n value_BLTnCalories -160.0nSaturated Fat -1.5nSaturated Fat (% Daily Value) -9.0nCholesterol (% Daily Value) 6.0nCarbohydrates (% Daily Value) -5.0nDietary Fiber (% Daily Value) 0.0nSugars -2.0nProtein 4.0nVitamin A (% Daily Value) 0.0nVitamin C (% Daily Value) 0.0nCalcium (% Daily Value) 0.0nIron (% Daily Value) 0.0n> value_Bacon <- apply(Bacon_menu,2,diff)n> data.frame(value_Bacon)n value_BaconnCalories -160nSaturated Fat -2nSaturated Fat (% Daily Value) -9nCholesterol (% Daily Value) 6nCarbohydrates (% Daily Value) -5nDietary Fiber (% Daily Value) 0nSugars -2nProtein 4nVitamin A (% Daily Value) 0nVitamin C (% Daily Value) 5nCalcium (% Daily Value) 0nIron (% Daily Value) 0n> value <- data.frame(value_classic,value_club,value_BLT,value_Bacon)n> valuen value_classic value_club value_BLT value_BaconnCalories -160.0 -160 -160.0 -160nSaturated Fat -1.5 -2 -1.5 -2nSaturated Fat (% Daily Value) -9.0 -8 -9.0 -9nCholesterol (% Daily Value) 6.0 6 6.0 6nCarbohydrates (% Daily Value) -4.0 -4 -5.0 -5nDietary Fiber (% Daily Value) 0.0 -1 0.0 0nSugars -2.0 -2 -2.0 -2nProtein 4.0 4 4.0 4nVitamin A (% Daily Value) 0.0 0 0.0 0nVitamin C (% Daily Value) 2.0 0 0.0 5nCalcium (% Daily Value) 0.0 0 0.0 0nIron (% Daily Value) 0.0 0 0.0 0n> n
可以看出對於Saturated Fat、Saturated Fat (% Daily Value)、Carbohydrates (% Daily Value)、Sugars這四個指標中,Crispy Chicken Sandwich的值比Grilled Chicken Sandwich高,而對於Cholesterol (% Daily Value)、Protein、Vitamin C (% Daily Value)這三個指標中,Grilled Chicken Sandwich的值更高一些,剩下的Dietary Fiber (% Daily Value)、Vitamin A (% Daily Value)、Calcium (% Daily Value)、Iron (% Daily Value)這四個指標中,Crispy Chicken Sandwich和Grilled Chicken Sandwich的數值是一樣的。
所以,具體選擇哪一種類型的Sandwich看自己更看重的是哪個指標,例如不需要太多的脂肪,就可以選擇Grilled Chicken Sandwich,如此等等。
4、怎麼從菜單中選擇最少數量的食品來你滿足一天的營養需求?
這裡主要以成年健康女性為例子進行選擇菜單,經過上網查詢得知健康的成年女性平均每餐應攝入熱卡含量為600~650卡的食物。
然後根據我自己的飲食習慣,早餐沒有特別要求,午餐和晚餐是必須要有肉,另外在麥當勞吃飯我喜歡點飲料來喝。
由於一餐的卡路里含量為600-650,所以選擇在這之間的早餐種類:
> ##選擇早餐中熱量介於600~650之間的食品。n> Breakfast <- by_category1[by_category1$Category=="Breakfast",]n> Breakfast <- Breakfast[Breakfast$Calories < 650 & Breakfast$Calories > 600,]n> BreakfastnSource: local data frame [2 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Breakfast Bacon, Egg & Cheese Bagel 620n2 Breakfast Big Breakfast with Egg Whites (Regular Biscuit) 640n> Breakfast <- Breakfast[1,] n> BreakfastnSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Breakfast Bacon, Egg & Cheese Bagel 620n> Breakfast <- sum(Breakfast$Calories)n> Breakfastn[1] 620n>n
所以,早餐選擇的是Bacon, Egg & Cheese Bagel和Big Breakfast with Egg Whites (Regular Biscuit),考慮到卡路里不能太高,所以最後選擇了Bacon, Egg & Cheese Bagel,卡路里熱量為620。
午餐的種類選擇了Beef & Pork、Desserts、Beverages、Coffee & Tea、Smoothies & Shakes,考慮到每一餐的卡路里限制在600~650之間,所以各個種類的選擇進行了排查:
> ##選擇lunch中熱量介於600~650之間的食品,Beef & Pork、Desserts、Beverages、Coffee & Tea、Smoothies & Shakes.n> lunch1 <- by_category1[by_category1$Category=="Beef & Pork",]n> lunch1 <- lunch1[lunch1$Calories < 300 & lunch1$Calories > 200,]n> lunch1nSource: local data frame [2 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beef & Pork Hamburger 240n2 Beef & Pork Cheeseburger 290n> lunch1 <- lunch1[-2,] ##選擇卡路里低的n> lunch1nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beef & Pork Hamburger 240n> lunch2 <- by_category1[by_category1$Category=="Desserts",]n> lunch2 <- lunch2[lunch2$Calories < 180 & lunch2$Calories > 100,]n> lunch2nSource: local data frame [2 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Desserts Chocolate Chip Cookie 160n2 Desserts Oatmeal Raisin Cookie 150n> lunch2 <- lunch2[-1,] ##選擇卡路里低的n> lunch2nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Desserts Oatmeal Raisin Cookie 150n> lunch3 <- by_category1[by_category1$Category=="Beverages",]n> lunch3 <- lunch3[lunch3$Calories < 150 & lunch3$Calories > 100,]n> lunch3nSource: local data frame [4 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beverages Coca-ColaClassic (Small) 140n2 Beverages DrPepper (Small) 140n3 Beverages Sprite (Small) 140n4 Beverages FatFreeChocolateMilkJug 130n> lunch3 <- lunch3[4,] ##選擇卡路里低的n> lunch3nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beverages FatFreeChocolateMilkJug 130n> lunch4 <- by_category1[by_category1$Category=="Coffee & Tea",]n> lunch4 <- lunch4[lunch4$Calories < 150 & lunch4$Calories > 100,]n> lunch4nSource: local data frame [8 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Coffee & Tea Sweet Tea (Child) 110n2 Coffee & Tea Nonfat Latte (Medium) 130n3 Coffee & Tea Nonfat Latte with Sugar Free French Vanilla Syrup (Small) 140n4 Coffee & Tea Regular Iced Coffee (Small) 140n5 Coffee & Tea Caramel Iced Coffee (Small) 130n6 Coffee & Tea Hazelnut Iced Coffee (Small) 130n7 Coffee & Tea French Vanilla Iced Coffee (Small) 120n8 Coffee & Tea Iced Coffee with Sugar Free French Vanilla Syrup (Medium) 120n> lunch4 <- lunch4[7,] ##選擇卡路里低的n> lunch4nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Coffee & Tea French Vanilla Iced Coffee (Small) 120n> lunch5 <- by_category1[by_category1$Category=="Smoothies & Shakes",]n> lunch5 <- lunch5[lunch5$Calories < 250 & lunch5$Calories > 200,]n> lunch5nSource: local data frame [3 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Smoothies & Shakes Blueberry Pomegranate Smoothie (Small) 220n2 Smoothies & Shakes Strawberry Banana Smoothie (Small) 210n3 Smoothies & Shakes Mango Pineapple Smoothie (Small) 210n> lunch5 <- lunch5[3,] ##選擇卡路里低的n> lunch5nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Smoothies & Shakes Mango Pineapple Smoothie (Small) 210n> lunch <- rbind(lunch1,lunch2,lunch3,lunch4,lunch5)n> lunchnSource: local data frame [5 x 3]nGroups: Category [5]nn Category Item Caloriesn <chr> <chr> <int>n1 Beef & Pork Hamburger 240n2 Desserts Oatmeal Raisin Cookie 150n3 Beverages FatFreeChocolateMilkJug 130n4 Coffee & Tea French Vanilla Iced Coffee (Small) 120n5 Smoothies & Shakes Mango Pineapple Smoothie (Small) 210n> lunch <- lunch[-3,] ##Beverages和Coffee & Tea選擇其中一個,考慮到下午還需要工作,所以選擇了Coffee & Tea。n> lunchnSource: local data frame [4 x 3]nGroups: Category [4]nn Category Item Caloriesn <chr> <chr> <int>n1 Beef & Pork Hamburger 240n2 Desserts Oatmeal Raisin Cookie 150n3 Coffee & Tea French Vanilla Iced Coffee (Small) 120n4 Smoothies & Shakes Mango Pineapple Smoothie (Small) 210n> lunch <- sum(lunch$Calories)n> lunchn[1] 720n> n
超過了每餐的卡路里的熱量,所以將Coffee & Tea去除了,因為已經有了Smoothies & Shakes,不喝飲料也沒關係。所以:
> lunch <- rbind(lunch1,lunch2,lunch3,lunch4,lunch5)n> lunch <- lunch[-c(3,4),]n> lunch <- sum(lunch$Calories)n> lunchn[1] 600n> n
所以,最後午餐選擇了Beef Hamburger、Oatmeal Raisin Cookie和Mango Pineapple Smoothie (Small),卡路里熱量為600。
最後是晚餐的選擇,主要從Chicken & Fish、Salads、Snacks & Sides、Beverages中選擇:
> ##dinner:Chicken & Fish、Salads、Snacks & Sides、Beveragesn> dinner1 <- by_category1[by_category1$Category=="Chicken & Fish",]n> dinner1 <- dinner1[dinner1$Calories < 300 & dinner1$Calories > 200,]n> dinner1nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Chicken & Fish Chicken McNuggets (6 piece) 280n> dinner2 <- by_category1[by_category1$Category=="Salads",]n> dinner2 <- dinner2[dinner2$Calories < 180 & dinner2$Calories > 100,]n> dinner2nSource: local data frame [2 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Salads Premium Bacon Ranch Salad (without Chicken) 140n2 Salads Premium Southwest Salad (without Chicken) 140n> dinner3 <- by_category1[by_category1$Category=="Beverages",]n> dinner3 <- dinner3[dinner3$Calories < 150 & dinner3$Calories > 100,]n> dinner3nSource: local data frame [4 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beverages Coca-ColaClassic (Small) 140n2 Beverages DrPepper (Small) 140n3 Beverages Sprite (Small) 140n4 Beverages FatFreeChocolateMilkJug 130n> dinner3 <- dinner3[4,] ##選擇卡路里低的n> dinner3nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Beverages FatFreeChocolateMilkJug 130n> dinner4 <- by_category1[by_category1$Category=="Snacks & Sides",]n> dinner4 <- dinner4[dinner4$Calories < 150 & dinner4$Calories > 100,]n> dinner4nSource: local data frame [1 x 3]nGroups: Category [1]nn Category Item Caloriesn <chr> <chr> <int>n1 Snacks & Sides Kids French Fries 110n> dinner <- rbind(dinner1,dinner2,dinner3,dinner4)n> dinner <- sum(dinner$Calories)n> dinnern[1] 800n> n
此時,卡路里熱量有點高,所以需要刪除部分食品:
> dinner <- rbind(dinner1,dinner2,dinner3,dinner4)n> dinnernSource: local data frame [5 x 3]nGroups: Category [4]nn Category Item Caloriesn <chr> <chr> <int>n1 Chicken & Fish Chicken McNuggets (6 piece) 280n2 Salads Premium Bacon Ranch Salad (without Chicken) 140n3 Salads Premium Southwest Salad (without Chicken) 140n4 Beverages FatFreeChocolateMilkJug 130n5 Snacks & Sides Kids French Fries 110n> dinner <- dinner[-3,] ##去除其中一份Saladsn> dinner <- sum(dinner$Calories)n> dinnern[1] 660n
此時,晚餐選擇了Chicken McNuggets (6 piece)、Premium Bacon Ranch Salad (without Chicken)、FatFreeChocolateMilkJug、Kids French Fries,卡路里是660熱量。
最後,查看一日三餐卡路里熱量:
> day <- data.frame(Breakfast,lunch,dinner)n> dayn Category Item Calories lunch dinnern1 Breakfast Bacon, Egg & Cheese Bagel 620 600 660n> n
這三餐的安排,午餐反而攝入的熱量最低了,晚餐的熱量最高了。
最後練習的結論:
通過這個練習,將前期的一些學習做了一次鞏固,挺好的,就是有些知識點忘記了,需要翻書重新查看,接下來準備將前期的一些內容做一次整理。
推薦閱讀:
TAG:数据分析 |