When you open the app.
Once you open the editor
Running your first line of code
cmd + enter
/ctrl + enter
.cmd + enter
/ctrl + enter
.Run
button
at top of screen.You need to tell your computer where to look at in order for it to find the documents, datasets, or files you want to use.
So PLEASE, make sure to organize your files in some way. Otherwise you are going to get lost.
option + command + c
shift + right click > Copy as Path
# Option 1: where you want
setwd("/Users/williampoirier/Dropbox/Website/files/uwo/R_Workshop_2025/rcode") # Mac
setwd("C:\Users\williampoirier\Dropbox\Website\files\uwo\R_Workshop_2025\rcode") # PC
# Option 2: where the R file is saved
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
code chunks
.First, make sure it is installed by running the following line in your console. You won’t have to do this again.
install.packages("rmarkdown")
Code must be place within code chuncks.
Initiate them by writing delimiters ```{r}
and
```
.
Or use a shortcut to initiate code chuncks:
Ctrl + Alt + i
Cmd + Option + i
R
Ships with many functions. But sometimes we need
other functions.install
and load
packages to add
functions to our library.install.packages("a package")
installs packages.library(a package)
loads (or makes available) the
functions from a specific package.# Installing packages (you do this only once)
install.packages("tidyverse")
install.packages("rio")
install.packages("devtools")
devtools::install_github("https://github.com/davidaarmstrong/damisc.git")
install.packages("knitr")
# Loading packages
library(tidyverse)
library(rio)
library(devtools)
library(DAMisc)
library(knitr)
Try running those operations yourself!
# Assignment (putting what's on the right in the box that's on the left.)
A <- 1
B <- "1"
C <- TRUE
D <- 3L
# Add
A+A
# [1] 2 (This is me writing what the console is going to output.)
# Substract
A-B
# Error in A - B : non-numeric argument to binary operator
# Why is this not working???
# Multiply
A*C
# [1] 1
# Why does this one work?
# Divide
A/A
# [1] 1
# Power
D^D
# [1] 27
# Square root
sqrt(D^D)
# [1] 5.196152
# Modulo
12%%5
# [1] 2
There are 4 main data types in R:
TRUE
(1) or
FALSE
(0) – abbreviate using T
and
F
# Take our original objects, class() will tell you what type of object they are.
class(A)
# [1] "numeric"
class(B)
# [1] "character"
# Note here that a number like 1 can be made to be a character with ""
# That's why A+B doesn't work, you can't add numbers and letters together.
class(C)
# [1] "logical"
class(D)
# [1] "integer"
# New objects
A <- 3
B <- 8
C <- 2
# Comparisons
A == B
# [1] FALSE
A != B
# [1] TRUE
A > B
# [1] FALSE
A < B
# [1] TRUE
A >= B
# [1] FALSE
A <= B
# [1] FALSE
# Logic
# The AND operator &
A > C & A < B
# [1] TRUE
# The OR operator |
A < C | A < B
# [1] TRUE
# Is A in the following?
A %in% 1:10
# [1] TRUE
# Not!
A > B
# [1] FALSE
!A > B
# [1] TRUE
# Vector (One dimension, one data type)
myVector <- c(1,2,3)
myVector
## [1] 1 2 3
# Matrix (Two dimensions, one data type)
myMatrix <- matrix(seq(1:16),nrow=4,ncol=4)
myMatrix
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
# Dataframe (Two dimensions, one data type per column)
myDataFrame <- data.frame(Fruits=c("Apple","Banana","Cherry","Kiwi"),
Scores=c(7,7,9,3),
MakesAGoodPie=c(T,T,T,F))
myDataFrame
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Lists (A bunch of stuff)
# (Parentheses arround assigment prints it uppon execution)
(Stuff <- list(myVector,myMatrix,myDataFrame))
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
##
## [[3]]
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Use Square Brakets []
# For a vector
myVector <- c("A","B","C")
# Get the first element of the vector
myVector[1]
## [1] "A"
# Get the first element from the end of the vector
myVector[-1]
## [1] "B" "C"
# Get elements 1 and 3
myVector[c(1,3)]
## [1] "A" "C"
# Get elements 1 through 3 (1,2,3)
myVector[1:3]
## [1] "A" "B" "C"
# Same idea for matrices and dataframes, but they have two dimensions!
# So you need two things, the row and the column number [row,column]
# Take the following dataframe
(myDataFrame <- data.frame(Fruits=c("Apple","Banana","Cherry","Kiwi"),
Scores=c(7,7,9,3),
MakesAGoodPie=c(T,T,T,F)))
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Extract the value in the cell on the first row and third column
myDataFrame[1,3]
## [1] TRUE
# Extract the first row, but with only columns from the second to last.
myDataFrame[1,2:ncol(myDataFrame)]
## Scores MakesAGoodPie
## 1 7 TRUE
# Extract rows from the first till the third, and the first and second column.
myDataFrame[1:3,1:2]
## Fruits Scores
## 1 Apple 7
## 2 Banana 7
## 3 Cherry 9
# $ used to extract a column
myDataFrame$Scores
## [1] 7 7 9 3
# Indexing can be used to create subsets
myDataFrame[myDataFrame$MakesAGoodPie==T,]
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
# Dimensions of objects
# How many elements in the vector?
length(myVector)
## [1] 3
# How many columns in a dataframe?
length(myDataFrame)
## [1] 3
ncol(myDataFrame)
## [1] 3
# How many rows in a dataframe?
nrow(myDataFrame)
## [1] 4
# How many rows and columns?
dim(myDataFrame)
## [1] 4 3
# What's going on in the data
# First 6 rows
head(myDataFrame)
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Last 6 rows
tail(myDataFrame)
## Fruits Scores MakesAGoodPie
## 1 Apple 7 TRUE
## 2 Banana 7 TRUE
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Can use the n argument to set the number of rows you want, here it's 2
tail(myDataFrame,n = 2)
## Fruits Scores MakesAGoodPie
## 3 Cherry 9 TRUE
## 4 Kiwi 3 FALSE
# Gives you statistics for each variable in your dataframe.
summary(myDataFrame)
## Fruits Scores MakesAGoodPie
## Length:4 Min. :3.0 Mode :logical
## Class :character 1st Qu.:6.0 FALSE:1
## Mode :character Median :7.0 TRUE :3
## Mean :6.5
## 3rd Qu.:7.5
## Max. :9.0
# Frequency of each value contained in a variable
table(myDataFrame$MakesAGoodPie)
##
## FALSE TRUE
## 1 3
# A basic histogram
hist(myDataFrame$Scores)
## Generating data
### X from a normal distribution
X <- rnorm(1000,mean=0,sd=1)
### W some group
W <- sample(c("Red","Blue","Orange"),size=1000,replace = T,prob=c(0.4,0.4,0.1))
### W_num the groups as numbers
W_num <- ifelse(W %in% c("Red","Blue"),0.3,-0.5)
### Y depends on X, W and some normally distributed error.
Y <- 0.6*X+W_num+X*W_num+rnorm(1000,mean=0,sd=1)
### Putting it all together
dat <- data.frame(id=1:100,X,Y,W)
## Vizualization with ggplot
ggplot(dat, aes(x=X,y=Y,color=W,fill=W))+
geom_point(alpha=0.3)+
geom_smooth(method="lm")+
scale_color_manual(values=c("blue","orange","red"))+
scale_fill_manual(values=c("blue","orange","red"))+
scale_x_continuous("X title",breaks = seq(-5,5,1))+
scale_y_continuous("Y title",breaks = seq(-5,5,1))+
theme_minimal()+
theme(legend.position = "top")
## `geom_smooth()` using formula = 'y ~ x'
Install and Load the carData
package.
How many columns and rows does the TitanicSurvival
dataset from carData
have?
What is the mean age of known passenger of the titanic? (Hint:
use the mean()
function and one of its arguments to ignore
the NAs.)
Create a graph of the distribution of age of the titanic
passengers. (Hint: use the hist()
function.)