Eli Boahen | Tutorial 1

Tutorial 1
Eli Boahen
Eli’s Walk through & completion of Tutorial 1
Author

Eli Boahen

Published

July 1, 2023

Code
library(tidyverse)

knitr::opts_chunk$set(echo = TRUE)

This Tutorial teaches 4 topics: Math, Logical Evaluations,

Math +, -, *, /

R can do math. By creating a code block in our markdown file using 3 of this symbol -> ( ` ), we can create mathematical expressions then press Crtl + Enter to run the single line of code.

Warning

Tip: ``` is the key on the top corner of your keyboard, not ’’’ which is 3 apostrophes

Code
12+2
[1] 14

Logical Data ==, >, <, >=, <=

R can do logic. Think of a TRUE or FALSE question: I have 2 apples. Do I have 2 apples?

Code
2==2
[1] TRUE

The line of code above will return TRUE

Now >, <, >=, <= are all testing for greater than or less than. > and < test just for greater than or less than. >= and <= are testing if something is greater than or equal to another value.

For Example:

Code
2>2
[1] FALSE
Code
2>=2
[1] TRUE

The first line of code returns FALSE because 2 is not greater than 2. But, the 2nd line returns TRUE because even though 2 is not greater than 2, 2 equals 2.

Inserting Code Into Your qmd File

Tip: We can make blocks of code by sandwiching it between this ( `) three times, and we can insert code into a sentence using those same symbols around a segment of code '''Like this''' or even 'Like this'.

Assigning Variables <-

On the same wavelength of math, R can assign variables. This is seen in other programming languages too! What’s different here–at least for me–is instead of using an equal sign like this x=2 we use this symbol <- like this

Code
x<-2
x
[1] 2

We can build on that to do things like

Code
x<-2
y<-x-3
y
[1] -1

Or even more intricate

Code
y >= x
[1] FALSE
Code
x <- y*y
x
[1] 1
Code
y >= x
[1] FALSE
Code
y <- y+2
y
[1] 1
Code
y >= x
[1] TRUE
Tip

This can work without spaces around the <- but it’s recommended to improve readability.

Vectors c()

So, variables can store 1 value, or the result of a mathematical expression

Code
x <- 4
x
[1] 4

Vectors store a list of values

Code
x <- c(2,4,6,8,10)
x
[1]  2  4  6  8 10

We can apply math and logic to these lists too Using the example from Tutorial 1

Code
scores <- c(14,13,12,15,16,14,15,10,8,12)
new_scores <- c(13,12,11,9,12,13,15,12,6,14)
new_scores
 [1] 13 12 11  9 12 13 15 12  6 14
Code
#new_grades variable is calculated by dividing the new scores by 15
grades <- new_scores/15
grades
 [1] 0.8666667 0.8000000 0.7333333 0.6000000 0.8000000 0.8666667 1.0000000
 [8] 0.8000000 0.4000000 0.9333333
Code
#multiply new_grades by 100 for clean percentages
percent_grades <- grades*100
percent_grades
 [1]  86.66667  80.00000  73.33333  60.00000  80.00000  86.66667 100.00000
 [8]  80.00000  40.00000  93.33333
Code
#average the two quizzes together into final grades
avg <- ( percent_grades + ((scores/17)*100) )/2
avg
 [1] 84.50980 78.23529 71.96078 74.11765 87.05882 84.50980 94.11765 69.41176
 [9] 43.52941 81.96078
Code
#weight the grades by 5 points
grade_weighted <- avg + 5
grade_weighted
 [1] 89.50980 83.23529 76.96078 79.11765 92.05882 89.50980 99.11765 74.41176
 [9] 48.52941 86.96078

The End

This is the end of Tutorial 1. My name is Eli, and I’m off to Tutorial 2. Thanks!