R markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

print("hello world")
## [1] "hello world"

Packages

It’s important to include everything R needs, including loading packages and data:

library(ggplot2)
library(kableExtra)
# diamonds is a dataset that is available in the ggplot2 package; 
# or load your own data using read.csv() etc. 
dat <- diamonds
head(dat)
## # A tibble: 6 x 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48

The diamonds dataset contains the prices and other attributes of almost 54,000 diamonds. It is available in the ggplot2 package as an example dataset.

Data analysis

Can conduct analysis in code chunks.

summary(diamonds$carat)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.2000  0.4000  0.7000  0.7979  1.0400  5.0100
mean_carat <- mean(diamonds$carat)
max_carat <- max(diamonds$carat)

Can then quote that the mean carat of the diamonds in the dataset was 0.7979397, which is substantially smaller than the maximum value of 5.01, indicating that this distribution is positively skewed.

Including Plots

You can also embed plots, as shown here. Note that the caption will be printed as part of the text, rather than in the code chunk.

\label{fig:plot}This is the plot caption.

This is the plot caption.

# code

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Including tables

The easiest way to include tables is by using knitr::kable(), which can create tables for HTML, PDF and Word outputs. Table captions can be included by passing the option caption to the function.

knitr::kable(diamonds[1:5, ], caption = 'This is the first table\'s caption.')
This is the first table’s caption.
carat cut color clarity depth table price x y z
0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
# Note the use of an escape character (\) to tell R that the apostrophe is not a quote mark.

kableExtra to make tables look nice in html version

We can neaten this up for html and word using the kableExtra() package to access more options to make the table look nice. Note that this will use the %>% pipe operator from tidyverse syntax. You’ll notice the difference in how we write the code using the pipe operator.

This is the second table’s caption.
carat cut color clarity depth table price x y z
0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75