Calculate your nutrients with my new package: NutrientData

June 15, 2016
By

[This article was first published on Renglish – 56north | Skræddersyet dataanalyse, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I have created a new package: NutrientData

This package contains data sets with the composition of Foods: Raw, Processed, Prepared. The source of the data is the USDA National Nutrient Database for Standard Reference, Release 28 (2015), a long with two functions to search and calculate nutrients.

`devtools::install_github("56north/NutrientData")`

Lets first have a look at the the top 20 calorie dense foods

```library(NutrientData) library(dplyr)```

``` data("ABBREV") # Load the data ```

```ABBREV %>% # Select the data arrange(-Energ_Kcal) %>% # Sort by calories per 100 g select(Food = Shrt_Desc, Calories = Energ_Kcal) %>% # Select relevant columns slice(1:20) %>% # Choose the top 20 ```

If you want to search for a specific ingredient you use the “search_ingredient” function. Lets search for raw onions:

`search_ingredient("onion,raw")`

You can also calculate the nutrient composition of several foods, like a simple yet delicious cabbage salad:

```ingredients <- c("CABBAGE,RAW", "MAYONNAISE,RED FAT,W/ OLIVE OIL", "ONIONS,RAW") grams <- c(100, 20, 10)```

``` ```

```calculate_nutrients(ingredients, grams) %>% select(Food = 1, Calories = 3, Protein = 4, Fat = 5, Carbs = 7) %>% # Select only a few variables for looks and rename ```

Dinner is served. I look forward to your feedback! And if anyone is up for it, then this is a package that is just begging for cool visualizations for nutrient composition along with a Shiny overlay.!

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.