What we are currently looking at is called 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:

# This is where you'd write some R code!

R Markdown documents have 2 main regions:

You’ll writing your code in the code chunks and answering written questions in the writing area!

How to write comments in code chunks:

# To write a comment in the code section, just begin that line with a #

# Anything written after a # will be ignored when the code is ran or the document is knitted!

# Comments in coding are very, very important for both other readers/user and yourself!
# If you think you may be commenting too much, you're likely commenting enough.
# Always comment more than you think you need!

When used outside of a code chunk, the # plays a different role. Instead of creating a comment, using multiple # signs will add headers and create different sections:

Section header

Subsection header

Subsubsection header

It’s important to know the role of the # depending on if it is in a code chunk or a writing area!

The Setup Code chunk

In the set up code chunk, three important parts of the code are written:

  1. global settings for the knitted document are specified that apply to all code chunks using knitr::opts_chunk$set(). If it looks odd to you, that’s because it is! We’ll explain what the :: and $ do later this week!

The four main settings I specify are:

I comment out warning = FALSE and message = FALSE until I’m ready to knit the final document. That way if messages and warnings appear while working on the code, I can see them. They are often useful when coding, but not what we typically want to see in final document!

The remaining two parts of the set up code chunk will be discussed later!

Objects in R

There are three main pieces of R code we will see in this course. The first we’ll discuss are called objects.

An object can be a number, character, logical/Boolean, and a myriad of other types. But those mentioned here are what we will be seeing most of the class and are the basic building blocks for other types of objects.

# Type 5 below this line
5
## [1] 5
# If we want a character (sometimes called strings), you need to place " " or ' ' around it
# Try entering cow:
'cow'
## [1] "cow"
# And we can display logical objects using TRUE or T and FALSE or F. Capitalization is important!
# Type TRUE below
TRUE
## [1] TRUE
# Then type F below!
F
## [1] FALSE

Saving objects

Anything you want to use later in R needs to be saved. You can save an object by using name <- value or value -> name

The <- and -> operator will save the value as an object in the global environment in the top right pane (default setting)

Try saving 5 to x using <- and y to “cow” using ->

# Save x as 5
x <- 5

# save "cow" as y
"cow" -> y

Important to note that when saving an object in R, it won’t display the result, just save it. If you want to see the result, you need to enter it again in the code chunk below where it was created:

To display the the object, you don’t need to type it in the same code chunk, but it does need to be typed somewhere after the line that created it.

# Save z as 10 then have it appear below the code chunk
z <- 10
z
## [1] 10

While z was created in the above code chunk, we can still reference it in a later code chunk. All the objects saved in each code chunk can be used in any code chunk below it!

If you want the object to show up in your answers and not necessarily below a code chunk, you can use 10 for it to appear in the knitted document in a writing area!

Storing multiple objects in a vector

A vector is a type of object that can hold multiple objects of the same type. To create a vector, you need to use c() and place a , between each new element of the vector:

# Try creating a vector that has 5, 10, 19 and save it as vec1
vec1 <- c(5, 10, 19)

# now create a vector that has F, F, F, M, M, F and save it as sex
sex <- c("F", "F", "F", "M", "M", "F")

# Finally, a vector with 3 trues and 2 falses
# Remember, for logicals, they need to be in all caps!
c(T, T, T, F, F)
## [1]  TRUE  TRUE  TRUE FALSE FALSE

You can also create vectors by referencing previously saved values!

Create a vector with the values saved in x and z (remember to use c() when creating a vector!)

c(x, z)
## [1]  5 10

Vectors have to store values of the same type. So what happens if we try to store the values saved in x, y, and z in the same vector? Let’s see!

c(x, y, z)
## [1] "5"   "cow" "10"

[write what happened here]

Vector sequence shortcut

If you want to create a vector that starts at a number, say a and ends at another number, b, and increases in increments of 1, you can use the : shortcut - a:b.

# Create a vector that starts at 1, ends at 10, and increases in increments of 1
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10

you can put a and b in either order. If you do b:a, then the sequence will decrease in increments of 1

# Create a vector that starts at 10, ends at 1, and decreases in increments of 1
10:1
##  [1] 10  9  8  7  6  5  4  3  2  1