R Markdown Exercises

R Markdown is a tool you will use extensively this semester, and it seems to inevitably be source of difficulty and frustration for students. So let’s practice all the key skills you’ll need now!

Exercise 1

Create a new R Markdown file in RStudio. Use HTML as the default output format, title it “Story of My Life,” and choose “use current date at render”, and set yourself as the author.

Delete everything except the metadata at the top of the file.

Exercise 2

Save and name your notebook, then write in a brief introduction to yourself. Make sure to include both something in bold and something in italics. I went with:

It all began on a cold, dry Wednesday in the great Denver, Colorado oh so many years ago.

Exercise 3

Then knit your R Markdown file to HTML.

Exercise 4

Open your knit markdown file in your browser and Print to PDF. Did it work?

Congratulations, you’re already way ahead of your classmates from last year.

Exercise 5

How did I know the weather on the day I was born? Great question! I got it here!

Come to think of it, why don’t you go do the same. Select “Daily Summary” observations, use the week of your birth as the date range, and use a relatively broad “Search For” option (I used “City,” but I was born in a major city with an international airport — if you can’t find a match when you first search just broaden the search to find the closest data).

Choose Custom GHCN-Daily CSV as download format, and on the next page Select All in the Select Data Types for Custom Output.

Exercise 6

In your R Markdown file, add a header (with two # signs) called “Finding My Birthday Weather.”

Below that, create an R cell (using a keyboard shortcut — don’t do it manually! You can hover your mouse over the new cell button if you forget the keyboard shortcut) and load the weather data in that cell.

At the top of your first code cell, make sure to add rm(list=ls()).

Exercise 7

Subset for the actual day of your birth (yes, you could have done that when downloading the data, but I wanted you to get more practice! :))

Exercise 8

You probably got more than one weather station in your download. Pick one (presumably the one closest to where you were born and/or the one with the most data — different weather stations collected different data).

Make sure to leave comments in your R code explaining your choices.

Exercise 9

Documentation for this data can be found here (Page 5).

In a new R code cell, write a print statement that summarizes the high and low temperatures on the day you were born. It should look something like:

print(paste("On the day I was born, the high temperature was", weather$HIGHTEMP))

Exercise 10

knit your notebook again. Congratulations! You’ve now created a document in which, given any changes to the input data, the results of your code will now auto-update.

Exercise 11

Now create a new heading below that code called “How many days have I been alive?”. Create a subsection under that (how do you make it a subsection?) called “The Formula”.

Exercise 12

Let’s write out some math. Start with two dollar signs, an x, and two dollar signs (put this in a regular text part of the document, not in a code block!).

When you wrap text in two dollar signs, R Markdown will render the result on its own line (one dollar sign on each side will render in line).

You should get a little popup that looks like this:

\[x\]

Exercise 13

Let’s add an i subscript. Add an underscore then an i. \(x_i\).

Exercise 14

Now add ,t to the subscript (we’re trying to set to \(x_{i,t}\)). Does the result look how you expect?

Exercise 15

So if you use an underscore, R Markdown will only treat the first character that follows as the subscript. If you want more than one character to be included in the subscript, you have to wrap the characters in {}: x_{i,t}. Do that.

Exercise 16

Now square your result. Use ^ to add a superscript. The same rules apply about what is included as applied to subscripts. The result should look like this: \(x_{i,t}^2\).

Exercise 17

But what if you want to use underscores (e.g., for a variable name)? Simple — put a backslash before the underscore. For example, to render \(my\_age\) in a formula I would type my\_age.

Exercise 18

Last trick. x is boring. Let’s use \(\beta\). How? \beta!

Want other symbols? Head over to detexify and draw the symbol you want or click symbols and search by name to get the code for putting in different characters! What R Markdown is doing is rendering anything listed on this page as mathmode that is not preceded by a \usepackage{} statement.

Exercise 19

Write a fun/crazy formula that gives your age of the form \(my\_age = ...\).

If you introduced variables, define then below the formula you wrote using single $ to get the results inline. e.g.,:

\[my\_age = (\sum_{i=0}^8 i) + z\]

Where \(z\) is the number of other people living in my house.

Exercise 20

Optional, but fun: try exporting to directly to PDF! Note this requires you to have installed LaTeX before.