Why are we here?

  • R is a powerful programming language. Rstudio lets you use R

  • R can rearrange data, summarize data, and make awesome figures

  • R is free, R is your friend, be patient with R

Course structure

Syllabus

class date topic class2 date2 topic2
1 Nov 6th orientation to Rstudio 7 Nov 27th Plots 2
2 Nov 7th cleaning data 8 Nov 28th Plots 3
3 Nov 13th Wrangling data 9 Dec 4th Catch-up day
4 Nov 14th Making tables 10 Dec 5th Making reports
5 Nov 20th Tables and Analyses 11 Dec 11th Making reports 2
6 Nov 21st Plots 1 12 Dec 12th EXAMS / Wild Card

Lets get started

We must first get organized

Lets get started

We must first get organized

Open Rstudio

R markdown

Tools > Global Options

Layout

Layout

Let's play around

Now is the time to ask questions if you have any

Organizing your code

  • The first code chunk for any markdown file is for packages and library (like below).
  • R comes with a handful of functions already
  • For additional functions you have to install them aka install.packages()
  • A package is a tool kit.
  • The tools in the kit are called functions.
#the # sign blanks out code
#You need to install.package only once, ever. Once installed your computer wont forget it
#that is why mine is blanked out with a # sign
#install.packages("tidyverse")
#install.packages("ggplot2")

#library() then tell your computer to remember the package you installed
#you will need to run this code everytime you start a new session
#there are 1000s of packages. tidyverse and ggplot2 are just two of the more useful common ones
library(tidyverse)
library(ggplot2)

Example Error

If you havent installed a package

  • This is a very common error
  • Meaning - you are trying to use a function (tool) for which you havent already installed the package (tool kit)

Identify the function's package

  • To figure out the name of the package, type the following and then look in the environment
?kable()

Install and library

Set working directtory

# <- R does not read code after the # sign
# setwd() tells r this is where your data is being stored on your computer
setwd("C:/Users/cyrus/OneDrive/Documents/R/input")

#read_csv brings in a csv file aka an excel spreadsheet
# the arrow sign assigns the excel spreadsheet to a name. 
#In this case I name the excel file "data"
data <- read_csv("test_data.csv")

You will code like a guru when you use keyboard shortcuts

  • ctrl + 1 Takes you to editor
  • ctrl + 2 Takes you to console
  • ctrl + enter Runs the line of code

Bread and butter commands

  • Use the keyboard shortcut to go to the console and run the following
#take a peak at the first few rows
head(data)
#try customizing output with head(data, 10)

#View the whole dataframe
View(data)

#take a peak at the last few rows
tail(data)

#check out the column names
names(data)

More Bread and butter commands

  • Use the keyboard shortcut to go to the console and run the following
#check the structure or type of data in a column.
str(data$Total1)

#for number or integer columns, check out some quick summary statistics
summary(data$Total1)
mean(data$`Patien's Age`)
?mean()
mean(data$`Patien's Age`, na.rm = TRUE)

#for categorical data, run table for a breakdown
table(data$Gender)

table(data$`Case Type`)

Many similar names. Next class we will learn how to change names

What to do if you need help

DONT PANIC !!!!

  • ask R for help
#you can put a question mark before any function and a help page pops up
?head()
  • Try a google search of the problem. Likely someone else asked a similar questions

  • Whatsapp or email the professor