---
title: "R Markdown example with cereal data"
author: "Chris Bilder"
output:  word_document
---

```{r echo = FALSE}
library(package = "knitr")
# Set up some options for how the document will look; the echo=FALSE part prevents the code from being included in the Word document
options(continue = " ")
opts_chunk$set(prompt = TRUE, comment = NA, background = "white", tidy = TRUE, tidy.opts = list(width.cutoff = 60, blank = FALSE))
```

# Cereal data example

The purpose of this document is to introduce some very basic aspects of using R Markdown. 

## Basics
A few years ago, I collected information on the nutritional content of dry cereals at a grocery store. This was done by first noting that one side of one aisle in many grocery stores usually contains all the cereals within a store. For example, below shows what the cereal aisle used to look like in the HyVee at 5020 N 27th St. before its recent renovation (image file needs to be in same folder as .Rmd file):

![](HyVee.jpg)

My research hypothesis was that there were different mean nutritional contents by shelf. For example, lower shelves may have more sugar content cereals than higher shelves.

The data used for this example was collected from a store a few years ago (not the HyVee in the picture). Note that there were only four shelves at this store and my sample size was 10 from each shelf. Below is how I read the data into R.

```{r}
# Data file needs to be put in same folder as .Rmd file or provide the full address to its location (either on your computer or on the Internet)
cereal <- read.csv(file = "http://www.chrisbilder.com/stat801/sections/2/cereal.csv")
head(cereal)
```

The shelves are numbered from lowest (1) to highest (4). 

We need to adjust the nutritional content variables (sugar_g, fat_g, and sodium_g) for the serving size because cereal boxes tend to have different serving sizes. Below is how I make the adjustment in R.

```{r}
cereal$sugar <- cereal$sugar_g/cereal$size_g
cereal$fat <- cereal$fat_g/cereal$size_g
cereal$sodium <- cereal$sodium_mg/cereal$size_g
head(cereal)

```

For example, the sugar value for Kellogg's Razzle Dazzle Rice Crispies is $\textrm{(sugar grams per serving)/(# of grams per serving size)}$ = 10/28 = 0.3571.

Alternatively, taking advantage of the `knitr` package, the calculation can be automatically done here using R: $$\textrm{(sugar grams per serving)/(# of grams per serving size)} = `r cereal$sugar_g[1]` / `r cereal$size_g[1]` = `r round(cereal$sugar_g[1]/cereal$size_g[1],4)`.$$ If these calculations were done outside of an equation, one could simply use something like: "The amount of sugar per gram is `r round(cereal$sugar_g[1]/cereal$size_g[1],4)`". 