You are on page 1of 10

install.

packages("readr")
WARNING: Rtools is required to build R packages but is not currently
installed. Please download and install the appropriate version of Rtools
before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Pawan
Chhatwal/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/readr_1.3.1.zip'
Content type 'application/zip' length 1585585 bytes (1.5 MB)
downloaded 1.5 MB

package ‘readr’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in


C:\Users\Pawan Chhatwal\AppData\Local\Temp\RtmpW003MH\
downloaded_packages
> library(readr)
>
> setwd(
+ "C:/Users/Pawan Chhatwal/Desktop/DESKTOP/BABI")
> getwd()
[1] "C:/Users/Pawan Chhatwal/Desktop/DESKTOP/BABI"
> install.packages("readxl")
WARNING: Rtools is required to build R packages but is not currently
installed. Please download and install the appropriate version of Rtools
before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Pawan
Chhatwal/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/readxl_1.3.1.zip'
Content type 'application/zip' length 1529082 bytes (1.5 MB)
downloaded 1.5 MB

package ‘readxl’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in


C:\Users\Pawan Chhatwal\AppData\Local\Temp\RtmpW003MH\
downloaded_packages
> library(readxl)
> library(readxl)
> Food_Nutrition_1 <- read_excel("Food Nutrition-1.xlsx")
> View(Food_Nutrition_1)
> Food_Nutrition<- read_excel("Food Nutrition-1.xlsx")
> > View(Food_Nutrition_1)
Error: unexpected '>' in ">"
> Food_Nutrition <- read_excel("Food Nutrition-1.xlsx")
> View(Food_Nutrition_1)
> head(Food_Nutrition,10)
# A tibble: 10 x 20
NDB_No Shrt_Desc `Water_(g)` Energ_Kcal `Protein_(g)`
<chr> <chr> <dbl> <dbl> <dbl>
1 01001 BUTTER,W~ 15.9 717 0.85
2 01002 BUTTER,W~ 16.7 718 0.49
3 01003 BUTTER O~ 0.24 876 0.28
4 01004 CHEESE,B~ 42.4 353 21.4
5 01005 CHEESE,B~ 41.1 371 23.2
6 01006 CHEESE,B~ 48.4 334 20.8
7 01007 CHEESE,C~ 51.8 300 19.8
8 01008 CHEESE,C~ 39.3 376 25.2
9 01009 CHEESE,C~ 37.0 404 22.9
10 01010 CHEESE,C~ 37.6 387 23.4
# ... with 15 more variables: `Lipid_Tot_(g)` <dbl>,
# `Ash_(g)` <dbl>, `Carbohydrt_(g)` <dbl>,
# `Fiber_TD_(g)` <dbl>, `Sugar_Tot_(g)` <dbl>,
# `Calcium_(mg)` <dbl>, `Iron_(mg)` <dbl>,
# `Magnesium_(mg)` <dbl>, `Phosphorus_(mg)` <dbl>,
# `Potassium_(mg)` <dbl>, `Sodium_(mg)` <dbl>,
# `Zinc_(mg)` <dbl>, `Copper_mg)` <dbl>,
# `Manganese_(mg)` <dbl>, `Selenium_(µg)` <dbl>
> tail(Food_Nutrition,20)
# A tibble: 20 x 20
NDB_No Shrt_Desc `Water_(g)` Energ_Kcal `Protein_(g)`
<chr> <chr> <dbl> <dbl> <dbl>
1 03934 BABYFOOD~ 82.6 68 1.26
2 03935 INF FORM~ 87.3 67 1.8
3 03936 INF FORM~ 88.1 63 1.4
4 03937 INF FORM~ 76 130 2.9
5 03938 INF FORM~ 2 524 12
6 03939 INF FORM~ 88 63 1.8
7 03940 INF FORM~ 76 126 3.6
8 03941 INF FORM~ 2 508 13.6
9 03942 INF FORM~ 87 68 1.71
10 03943 INF FORM~ 3 509 12.7
11 03944 INF FORM~ 87 69 1.92
12 03945 INF FORM~ 2.25 520 14.4
13 03946 INF FORM~ 87 68 1.48
14 03947 INF FORM~ 75.9 128 2.73
15 03948 INF FORM~ 2.2 520 11.1
16 03949 INF FORM~ 87.8 66 1.36
17 03950 INF FORM~ 2.25 522 10.9
18 03951 INF FORM~ 76.2 127 2.64
19 03952 INF FORM~ 75.8 128 3.13
20 03953 INF FORM~ 87.5 66 1.61
# ... with 15 more variables: `Lipid_Tot_(g)` <dbl>,
# `Ash_(g)` <dbl>, `Carbohydrt_(g)` <dbl>,
# `Fiber_TD_(g)` <dbl>, `Sugar_Tot_(g)` <dbl>,
# `Calcium_(mg)` <dbl>, `Iron_(mg)` <dbl>,
# `Magnesium_(mg)` <dbl>, `Phosphorus_(mg)` <dbl>,
# `Potassium_(mg)` <dbl>, `Sodium_(mg)` <dbl>,
# `Zinc_(mg)` <dbl>, `Copper_mg)` <dbl>,
# `Manganese_(mg)` <dbl>, `Selenium_(µg)` <dbl>
> summary(Food_Nutrition)
NDB_No Shrt_Desc Water_(g)
Length:600 Length:600 Min. : 0.20
Class :character Class :character 1st Qu.:39.24
Mode :character Mode :character Median :76.16
Mean :60.35
3rd Qu.:85.96
Max. :99.90
Energ_Kcal Protein_(g) Lipid_Tot_(g)
Min. : 0.0 Min. : 0.000 Min. : 0.0000
1st Qu.: 63.0 1st Qu.: 1.708 1st Qu.: 0.7125
Median :106.5 Median : 3.915 Median : 3.4950
Mean :187.1 Mean : 8.686 Mean : 8.9175
3rd Qu.:315.8 3rd Qu.:12.500 3rd Qu.:13.0500
Max. :876.0 Max. :84.080 Max. :99.4800
Ash_(g) Carbohydrt_(g) Fiber_TD_(g)
Min. : 0.000 Min. : 0.00 Min. : 0.000
1st Qu.: 0.490 1st Qu.: 5.52 1st Qu.: 0.000
Median : 0.895 Median :10.80 Median : 0.000
Mean : 2.547 Mean :19.34 Mean : 2.579
3rd Qu.: 3.260 3rd Qu.:19.08 3rd Qu.: 1.200
Max. :99.800 Max. :86.68 Max. :53.200
Sugar_Tot_(g) Calcium_(mg) Iron_(mg)
Min. : 0.0000 Min. : 0.00 Min. : 0.0000
1st Qu.: 0.5575 1st Qu.: 23.75 1st Qu.: 0.1875
Median : 4.7850 Median : 102.50 Median : 0.4150
Mean : 9.0940 Mean : 263.14 Mean : 4.3583
3rd Qu.:10.6775 3rd Qu.: 353.00 3rd Qu.: 1.8925
Max. :74.4600 Max. :2240.00 Max. :123.6000
Magnesium_(mg) Phosphorus_(mg) Potassium_(mg)
Min. : 0.0 Min. : 0.0 Min. : 0
1st Qu.: 8.0 1st Qu.: 35.0 1st Qu.: 95
Median : 13.0 Median : 101.0 Median : 141
Mean : 41.7 Mean : 189.4 Mean : 311
3rd Qu.: 33.0 3rd Qu.: 269.2 3rd Qu.: 255
Max. :711.0 Max. :1349.0 Max. :4740
Sodium_(mg) Zinc_(mg) Copper_mg)
Min. : 0.00 Min. : 0.000 Min. :0.000
1st Qu.: 21.75 1st Qu.: 0.300 1st Qu.:0.026
Median : 50.00 Median : 0.675 Median :0.050
Mean : 349.95 Mean : 1.536 Mean :0.151
3rd Qu.: 171.00 3rd Qu.: 2.373 3rd Qu.:0.110
Max. :38758.00 Max. :17.460 Max. :2.467
Manganese_(mg) Selenium_(µg)
Min. : 0.000 Min. : 0.00
1st Qu.: 0.020 1st Qu.: 1.80
Median : 0.140 Median : 3.70
Mean : 1.011 Mean : 12.43
3rd Qu.: 0.677 3rd Qu.: 14.50
Max. :60.127 Max. :208.10
> Food_Nutrition$NDB_No=factor("Food_Nutrition$NDB_No")
> test<-head(Food_Nutrition$`Protein_(g)`,10)
> test
[1] 0.85 0.49 0.28 21.40 23.24 20.75 19.80 25.18 22.87
[10] 23.37
> Food_Nutrition[1:5,1:5]
# A tibble: 5 x 5
NDB_No Shrt_Desc `Water_(g)` Energ_Kcal `Protein_(g)`
<fct> <chr> <dbl> <dbl> <dbl>
1 Food_Nutr~ BUTTER,WIT~ 15.9 717 0.85
2 Food_Nutr~ BUTTER,WHI~ 16.7 718 0.49
3 Food_Nutr~ BUTTER OIL~ 0.24 876 0.28
4 Food_Nutr~ CHEESE,BLUE 42.4 353 21.4
5 Food_Nutr~ CHEESE,BRI~ 41.1 371 23.2
> class(Food_Nutrition$`Sodium_(mg)`)
[1] "numeric"
>
> Food_Nutrition$EPW<-Food_Nutrition$Energ_Kcal/Food_Nutrition$`Water_(g)`
> head(Food_Nutrition$EPW)
[1] 45.179584 42.942584 3650.000000 8.323509
[5] 9.024568 6.897976
> dim(Food_Nutrition)
[1] 600 21
> food_new<-Food_Nutrition[Food_Nutrition$Energ_Kcal<500,]
> dim(food_new)
[1] 564 21
> less calorie<-Food_Nutrition[Food_Nutrition$Energ_Kcal<500,]
Error: unexpected symbol in "less calorie"
> less_calorie<-Food_Nutrition[Food_Nutrition$Energ_Kcal<500,]
> #11-Plot a Bar Plot between Enrg_Kcal and Water using the new subset
created
> install.packages("ggplot2")
WARNING: Rtools is required to build R packages but is not currently
installed. Please download and install the appropriate version of Rtools
before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Pawan
Chhatwal/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
also installing the dependencies ‘stringi’, ‘colorspace’, ‘stringr’,
‘labeling’, ‘munsell’, ‘RColorBrewer’, ‘gtable’, ‘lazyeval’, ‘plyr’,
‘reshape2’, ‘scales’, ‘viridisLite’, ‘withr’

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/stringi_1.4.3.zip'
Content type 'application/zip' length 15289558 bytes (14.6 MB)
downloaded 14.6 MB
trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/colorspace_1.4-1.zip'
Content type 'application/zip' length 2549158 bytes (2.4 MB)
downloaded 2.4 MB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/stringr_1.4.0.zip'
Content type 'application/zip' length 214082 bytes (209 KB)
downloaded 209 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/labeling_0.3.zip'
Content type 'application/zip' length 61756 bytes (60 KB)
downloaded 60 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/munsell_0.5.0.zip'
Content type 'application/zip' length 244035 bytes (238 KB)
downloaded 238 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/RColorBrewer_1.1-2.zip'
Content type 'application/zip' length 55498 bytes (54 KB)
downloaded 54 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/gtable_0.3.0.zip'
Content type 'application/zip' length 433090 bytes (422 KB)
downloaded 422 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/lazyeval_0.2.2.zip'
Content type 'application/zip' length 170895 bytes (166 KB)
downloaded 166 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/plyr_1.8.4.zip'
Content type 'application/zip' length 1297314 bytes (1.2 MB)
downloaded 1.2 MB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/reshape2_1.4.3.zip'
Content type 'application/zip' length 626839 bytes (612 KB)
downloaded 612 KB
trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/scales_1.0.0.zip'
Content type 'application/zip' length 1064823 bytes (1.0 MB)
downloaded 1.0 MB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/viridisLite_0.3.0.zip'
Content type 'application/zip' length 60479 bytes (59 KB)
downloaded 59 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/withr_2.1.2.zip'
Content type 'application/zip' length 150975 bytes (147 KB)
downloaded 147 KB

trying URL
'https://cran.rstudio.com/bin/windows/contrib/3.5/ggplot2_3.1.1.zip'
Content type 'application/zip' length 3621580 bytes (3.5 MB)
downloaded 3.5 MB

package ‘stringi’ successfully unpacked and MD5 sums checked


package ‘colorspace’ successfully unpacked and MD5 sums checked
package ‘stringr’ successfully unpacked and MD5 sums checked
package ‘labeling’ successfully unpacked and MD5 sums checked
package ‘munsell’ successfully unpacked and MD5 sums checked
package ‘RColorBrewer’ successfully unpacked and MD5 sums checked
package ‘gtable’ successfully unpacked and MD5 sums checked
package ‘lazyeval’ successfully unpacked and MD5 sums checked
package ‘plyr’ successfully unpacked and MD5 sums checked
package ‘reshape2’ successfully unpacked and MD5 sums checked
package ‘scales’ successfully unpacked and MD5 sums checked
package ‘viridisLite’ successfully unpacked and MD5 sums checked
package ‘withr’ successfully unpacked and MD5 sums checked
package ‘ggplot2’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in


C:\Users\Pawan Chhatwal\AppData\Local\Temp\RtmpW003MH\
downloaded_packages
> library(ggplot2)
>
> #11-Plot a Bar Plot between Enrg_Kcal and Water using the new subset
created
> qplot(Food_Nutrition$Energ_Kcal,Food_Nutrition$`Water_(g)`,
+ main = "Plot between Energ_Kcal Vs Water ")
>
> qplot(less_calorie$Energ_Kcal,less_calorie$`Water_(g)`,
+ main = "Plot between Energ_Kcal Vs Water ")
> plot(less_calorie$Energ_Kcal,less_calorie$`Water_(g)`,
+ main = "Plot between Energ_Kcal Vs Water ")
> #12-Plot a histogram of Sugar_tot variable using the new subset
> qplot(Food_Nutrition$`Sugar_Tot_(g)`)
`stat_bin()` using `bins = 30`. Pick better value with
`binwidth`.
> #12-Plot a histogram of Sugar_tot variable using the new subset
>
> qplot(less_calorie$`Sugar_Tot_(g)`)
`stat_bin()` using `bins = 30`. Pick better value with
`binwidth`.
> Find the top 10 products based on following:
Error: unexpected symbol in "Find the"
> #13.1- Higher the Energy_Kcal, higher the ranking
> highenergy = order(Food_Nutrition$Energ_Kcal>,)
Error: unexpected ',' in "highenergy = order(Food_Nutrition$Energ_Kcal>,"
>
> x=Food_Nutrition
> y=Food_Nutrition[order(-Food_Nutrition$Energ_Kcal,)
+ head(y,$shrt_Desc,10)
Error: unexpected symbol in:
"y=Food_Nutrition[order(-Food_Nutrition$Energ_Kcal,)
head"
> head(y,$shrt_Desc,10)
Error: unexpected '$' in "head(y,$"
> head(y$shrt_Desc,10)
Error in head(y$shrt_Desc, 10) : object 'y' not found
> head(y_Desc,10)
Error in head(y_Desc, 10) : object 'y_Desc' not found
> ### Cannot do 13
> #14-Create a subset of the data where product_desc contains "CHEESE" and
> # list down the summary statistics of the subset
> ###Cnnot do 14 and 15
> # As CSV file
> write.csv(Food_Nutrition, "As_csv_Food_Nutrition_2019.csv", row.names =
FALSE)
>
> saveRDS(Food_Nutrition, "As_RDS_Food_Nutrition_2019.RDS")
>

You might also like