Preparation
Create File
In RStudio, choose File > New File > R Markdown…
Title: R Assignment
Author: your SSE registration number
Date: today’s date
Default Output Format: PDF
Click OK.
Your header should look like below (but with your SSE registration number instead of 12345 and today’s date instead of YYYY-MM-DD):
---
title: "R Assignment"
author: 12345
date: "YYYY-MM-DD"
output: pdf_document ---
Now, delete everything below the header. Do not delete the three dashes (- - -) above or below the header.
Choose File > Save as… and save your file with an appropriate name and in a folder that you can easily find on your computer.
Knit your file. Knitting means that you let RStudio produce a PDF file. You can find a Knit button in the menu above your code, or you can go to File > Knit Document. (Knitting will take a few moments, so wait a bit for it to produce the PDF file.) If you encounter problems when trying to knit your file, your installation of LaTeX did not finish properly. Please go back to the installation chapter and try again. When the knitting part works you can go to the next step.
Copy the chunk below and paste it into your R Markdown file, below your header. This chunk has some general settings for your document and opens the necessary packages that you have already installed.
```{r, include=FALSE}
knitr::opts_chunk$set(echo = FALSE) # Show/Hide chunks in output
knitr::opts_chunk$set(message = FALSE) # Show/Hide error messages
knitr::opts_chunk$set(comment = NA) # Show/Hide comments
library(readxl) # For loading Excel files
library(kableExtra) # For table formatting
library(ggplot2) # For graphs
library(tinytex) # For making a pdf
library(tibble) # For the temporary data
library(dplyr)
library(tidyr)
library(RColorBrewer) # For heat map
options(scipen=999) # Fix y-axis formats (for R in general) ```
You should check that your packages have been installed properly. You can do this by pressing the Run button (a green “Play” button) in the top right corner of the chunk. If one or more of the libraries do not load properly, please go back to the installation chapter and try again.
If you want to have a quick tour of R Markdown, please see R Markdown Quick Tour.
Load Data
Download your personal data set from the course web. The file has the format 12345.xlsx, where 12345 corresponds to your SSE registration number. It is important that you save the file in the same folder as your R Markdown file! Otherwise RStudio will not find your data file when your try to load it. When you have saved the file, you can have a look at the data in Excel, but do not change anything in the Excel file.
To load the data into RStudio, copy and paste the following code chunk into your file, change 12345 to your SSE registration number so that RStudio can find the right Excel file, and then run the chunk by clicking the Run button in the top right corner of the chunk.
```{r}
data<-read_excel("12345.xlsx",1)
data$date<-as.Date(data$date)
data[data<0]<-0 # To simplify: We set negative values to zero ```
The data should now be loaded into RStudio. You should see that the “data” has popped up in the top right window of RStudio, under the Environment tab. Make sure that the data has been loaded properly by clicking it. You should now see the data in a new tab.
When the above is working, we are ready to start making some analysis on the data…