Code
library(tidyverse)
::opts_chunk$set(echo = TRUE) knitr
Eli Boahen
July 1, 2023
This Tutorial teaches 4 topics: Math, Logical Evaluations,
+, -, *, /
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.
Tip: ``` is the key on the top corner of your keyboard, not ’’’ which is 3 apostrophes
==, >, <, >=, <=
R can do logic. Think of a TRUE
or FALSE
question: I have 2 apples. Do I have 2 apples?
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:
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.
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'
.
<-
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
We can build on that to do things like
Or even more intricate
[1] FALSE
[1] 1
[1] FALSE
[1] 1
[1] TRUE
This can work without spaces around the <-
but it’s recommended to improve readability.
c()
So, variables can store 1 value, or the result of a mathematical expression
Vectors store a list of values
We can apply math and logic to these lists too Using the example from Tutorial 1
[1] 13 12 11 9 12 13 15 12 6 14
[1] 0.8666667 0.8000000 0.7333333 0.6000000 0.8000000 0.8666667 1.0000000
[8] 0.8000000 0.4000000 0.9333333
[1] 86.66667 80.00000 73.33333 60.00000 80.00000 86.66667 100.00000
[8] 80.00000 40.00000 93.33333
[1] 84.50980 78.23529 71.96078 74.11765 87.05882 84.50980 94.11765 69.41176
[9] 43.52941 81.96078
[1] 89.50980 83.23529 76.96078 79.11765 92.05882 89.50980 99.11765 74.41176
[9] 48.52941 86.96078
This is the end of Tutorial 1. My name is Eli, and I’m off to Tutorial 2. Thanks!
---
title: "Eli Boahen | Tutorial 1"
author: "Eli Boahen"
description: "Eli's Walk through & completion of Tutorial 1"
date: "07/01/2023"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
categories:
- Tutorial 1
- Eli Boahen
---
```{r}
#| label: setup
#| warning: false
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.
:::{.callout-warning}
Tip: ``` is the key on the top corner of your keyboard, not ''' which is 3 apostrophes
:::
```{r}
12+2
```
## Logical Data `==, >, <, >=, <=`
R can do logic. Think of a `TRUE` or `FALSE` question:
I have 2 apples. Do I have 2 apples?
```{r}
2==2
```
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:
```{r}
2>2
2>=2
```
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.
:::{.callout-tip}
## 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
```{r}
x<-2
x
```
We can build on that to do things like
```{r}
x<-2
y<-x-3
y
```
Or even *more* intricate
```{r}
y >= x
x <- y*y
x
y >= x
y <- y+2
y
y >= x
```
:::{.callout-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
```{r}
x <- 4
x
```
Vectors store a list of values
```{r}
x <- c(2,4,6,8,10)
x
```
We can apply math and logic to these lists too
Using the example from Tutorial 1
```{r}
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
#new_grades variable is calculated by dividing the new scores by 15
grades <- new_scores/15
grades
#multiply new_grades by 100 for clean percentages
percent_grades <- grades*100
percent_grades
#average the two quizzes together into final grades
avg <- ( percent_grades + ((scores/17)*100) )/2
avg
#weight the grades by 5 points
grade_weighted <- avg + 5
grade_weighted
```
## The End
This is the end of Tutorial 1. My name is Eli, and I'm off to Tutorial 2. Thanks!