---
title: "Introduction to Markdown and RMarkdown (and R)"
author: "J. Doe"
date: "3/2/2020"
output: html_document
---
# Introducing Markdown and RMarkdown
## Markdown
It's helpful to get a good sense of what you can do using Markdown in the text blocks (like this one!) of an RMarkdown document. Notice the use of the hashmark # to show a header: one marks a section, two marks a subsection, three marks a subsubsection, etc.
### Emphasizing Text
In these text blocks, you can *use asterisks to mark something as italics*, and you can use **double asterisks, to make things bold**.
### Links
Additionally, if you can make [a link](https://daringfireball.net/projects/markdown/syntax) using paired brackets and parentheses.
Use the above link if you want to find out more about Markdown, but this should be enough for our class.
## RMarkdown
The variant of Markdown called RMarkdown also allows for code chunks. You'll indicate code chunks using three backtick marks at the beginning and end of the code block; at the start of the code block, put a lowercase "r" between curly braces (```{r}), as you can see below. (The easiest way to do all of this in one go is "Ctrl+Alt+i" on Windows, "Command+Option+i" on a Mac.)
It's also possible to have inline code, which is useful if you want to refer to the value of an object in your text, but we'll save that for another time.
# Introducing R (recap)
## Basic Operations
R is a calculator using simple notation for simpler arithmetic.
```{r}
# New functions: basic arithmetic
# Adding two numbers
1 + 1
# Order of operations
12 * (10 + 1)
# Scientific notation
3.5E03 + 4E-01
# Pi is a built-in constant
pi
# Yes, you can divide by zero
1001/0
# Exponents
15^2
```
We can also use functions to calculate some more advanced math.
```{r}
# New functions: abs(), sin(), sqrt(), floor(), ceiling(), round()
# Absolute value
abs(-10)
# Trigonometry
sin(pi/2)
# Square root
sqrt(225)
# Round to an integer by rounding down, rounding up, or rounding by closeness
floor(3.1415)
ceiling(pi)
round(pi)
```
## Program with Objects
We can use objects as if they are simple variables in equations.
```{r}
# Define two objects
x <- 5
y <- 2 * x
# ... and use them in basic a calculation.
x + y
```
We can even use them in more complicated ways, using any of the commands we'd use with a number.
```{r}
x * y
y / x
(x * y) / x
```
Objects can easily be reassigned values. Here's the value beforehand:
```{r}
# New function: message()
message(x)
```
And here we reasign the value, followed by proof:
```{r}
x <- "Hello world"
message(x)
x
```
Notice that the `message()` function simply prints the object's value, while using the name of the object prints it on a special line, and sometimes with quotation marks.
## Working with Vectors
Vectors allow us to assign more than one value to one object.
```{r}
# New function: c()
my_favorite_words <- c("flabbergast","centrifuge","defenestrate")
my_favorite_words
nums1 <- c(1,4,2,8,11,100,8)
nums1
```
Notice that these are the same as the following vectors, which otherwise differ only in spacing of the definition. R can be pretty forgiving about spacing and line breaks, if you're using them at the right times.
```{r}
my_favorite_words <- c("flabbergast",
"centrifuge",
"defenestrate")
my_favorite_words
nums1 <- c(1, 4, 2, 8,
11, 100, 8)
nums1
```
From here, we can get the sum of a vector of numbers:
```{r}
# New function: sum()
sum(nums1)
```
And we can easily get other summary values:
```{r}
# New functions: mean(), sd(), length()
# Find the average (or "mean")
mean(nums1)
# Find the standard deviation...
sd(nums1)
# Find the length of the object
length(nums1)
```
## Vectorized Operations
Vectors make it easy to perform batch operations on a whole series of values.
```{r}
# Make two vectors,
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(11, 12, 13, 14, 15)
# Add a number, element-wise
vec1 + 10
# Element-wise quadratic:
vec1^2
# Pair-wise multiplication:
vec1 * vec2
# Pair-wise division
vec1 / vec2
# Pair-wise difference:
vec2 - vec1
# Pair-wise sum:
vec1 + vec2
# Compare the pair-wise sum to the sum of both vectors:
sum(vec1) + sum(vec2)
```
## Applying Multiple Functions at Once
(this slide is left for you to finish)
```{r}
# New functions: min() and max()
```
## Character Vectors
(this slide is left for you to finish)
```{r}
# New function: sort(), nchar()
```
## Working with Matrices
(this slide is left for you to finish)
```{r}
# New function: matrix()
```
## Matrix Arithmetic
(these slides are left for you to finish)
```{r}
# New functions: diag(), rowSums(), colSums(), t()
```
## Generating Data
(these slides are left for you to finish)
```{r}
# New operation and new functions: :, seq(), rep()
```
## Random Numbers and Random Items
```{r}
# New functions: runif(), sample()
# Ten random numbers between 0 and 1
runif(10)
# Five random numbers between 100 and 1000
runif(5, 100, 1000)
# Five random items from a given numeric vector
my_100 <- 1:100
sample(my_100, 5)
# Five random animals from a character vector
my_animals <- c("antelope", "bunny", "cheetah",
"deer", "elephant", "fish",
"giraffe", "hyena", "ibis",
"jellyfish", "kangaroo", "lemur",
"mongoose", "narwhal", "octopus",
"prairie dog", "quokka",
"rhesus macaque", "snake",
"tarantula", "unicorn", "vulture",
"walrus", "x-ray tetra", "yak",
"zebu")
sample(my_animals, 5)
```
# Knitting and Publishing
To see your document finished, hit the "knit" button in the toolbar. It'll compile the document into a webpage.
As a bonus, you'll see that the following lines also give you a picture embedded in the webpage. Use them as a guide to change the last few lines of your RMarkdown header, and then save and knit your document again to see how it changes.
![](https://jmclawson.net/bigdata/darkness/YAML-toc.png)