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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

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

Enter this data set, giving it the name “tomatodata”:

tomatodata <-  c( 6.3, 7.2, 6.8, 5.4, 6.5, 5.9, 6.6, 6.5, 7.1, 7.0) 

Take the average:

mean(tomatodata)
## [1] 6.53

Take the median:

median(tomatodata)
## [1] 6.55

Find the standard deviation:

sd(tomatodata)
## [1] 0.5578729

Find the largest number:

max(tomatodata)
## [1] 7.2

Find the smallest number:

min(tomatodata)
## [1] 5.4

Find the 30th percentile:

quantile(tomatodata, 0.30)
##  30% 
## 6.44

Find the 5-number summary (min, 25th percentile, median, 75th percentile, max):

quantile(tomatodata)
##   0%  25%  50%  75% 100% 
## 5.40 6.35 6.55 6.95 7.20

Find the interquartile range (75th percentile minus the 25th percentile)

IQR(tomatodata)
## [1] 0.6

Make a histogram:

hist(tomatodata)

Make a box plot (display the five number summary):

boxplot(tomatodata)

Make a horizontal box plot:

boxplot(tomatodata, horizontal=TRUE)

Make the output say “Tomato data is awesome!”

print("Tomato data is awesome!")
## [1] "Tomato data is awesome!"

Make the output say “The mean tomato size is 6.53”

print(paste("The mean tomato size is",mean(tomatodata)),sep ="")
## [1] "The mean tomato size is 6.53"

Install the tidyverse package in the console below markdown

install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)

Load the tidyverse library and diamonds data into the markdown

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Load the Mtcars data and view the first 6 rows

data(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Use tidyverse language to make a histogram of any numeric variable in Mtcars:

ggplot(data = mtcars, aes(x = mpg)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.