Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Aariq committed Aug 21, 2023
2 parents bf8b1a1 + e48351b commit a559c5f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lessons/6-data-manipulation/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ footer: "[Reproduciblity & Data Science in R](https://cct-datascience.github.io/

# Installing packages

```{r, eval = F, include =T, echo =T}
```
install.packages("dplyr")
install.packages("tidyr")
Expand Down
3 changes: 1 addition & 2 deletions lessons/8-intermediate-r-2/notes.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Learners will learn how to make their R code more reproducible using for loops a

### Review

Can share their plans for apply skills to their research.
Lead optional discussion on when to use base R vs. tidyverse packages/functions.
Can share their plans for apply skills to their research. Lead optional discussion on when to use base R vs. tidyverse packages/functions.

### Modifications

Expand Down
86 changes: 86 additions & 0 deletions lessons/8-intermediate-r-2/slides.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: "Intermediate R: iteration and mapping"
subtitle: "Session 8"
format: revealjs
execute:
echo: true
engine: knitr
chalkboard: true
footer: "[Reproduciblity & Data Science in R](https://cct-datascience.github.io/repro-data-sci/)"
---

# Review

- Were you able to find a section of your own code to update using `ifelse` or a function?

# Review

- What ideas do you have about possible showcase projects?
- What questions do you have about that?

# Warm-up: create a new branch for today's work

# Learning objectives

- Understand how iterating can improve your code
- Iterate using `for` loops
- Iterate using `apply` functions <!--# Or purrr -->

# What do we mean by iteration?

# Iteration by hand

(visual of copy-paste)

# Why use code to iterate?

- Less code: fewer bugs, easier to understand
- Easier to apply to new situations
- Faster to write, maybe faster to run

# Iteration 3 (or more!) ways

- For loops
- Functions + `apply` or `map`

# Coding time

- For loop
- For loop with different indices

# For loops: pros and cons

- Pros
- Simple to write and read
- Cons
- Can be slow
- Still somewhat inefficient to write
- Still somewhat fragile

# The `apply` family

- Provide the function name and inputs to the `apply` functions
- **applies** the function to a vector, list, or matrix of inputs

# Coding time

`apply`

# `purrr`

- Similar to apply, but easier to read

# Coding time

`purrr`

# Coding time

Commit this week's work and create a new PR.


# Homework

- Identify a section of your code that could be made more efficient using a for loop or iterating using a function
- Continue thinking about showcase projects.

0 comments on commit a559c5f

Please sign in to comment.