── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.1 ✔ stringr 1.5.2
✔ ggplot2 4.0.0 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.1.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nycflights13)
Based on a recommendation from the Productive R Workflow course, this page will be a sorting through of how to create functions in R…but the Seahawks are playing and I need to go pick up the pizza…
…one week later…
As you can see at the top of the post, I am following the R4DS (2e) online book (Wickham, Çetinkaya-Rundel, and Grolemund 2023), but focusing on ch25, so if you aren’t familiar with R, you might need to back up a bit in the book.
The need for me here is that the data is currently coming in for my PhD, and it is time to get serious about managing my workflow and R coding practices. Creating functions enhances reproducibility as there will only be one place for a particular task to run, rather than my copy/pasta/edit technique to date. This will not only create a more reproducible paper, but it will be cleaner in the index.qmd doc because I will only need to call the function, rather than include the entire code inline.
Vector Functions
vector functions
take one or more vectors and return a vector as a result. Example from R4DS:
# A tibble: 5 × 4
a b c d
<dbl> <dbl> <dbl> <dbl>
1 0 -0.324 0 0.420
2 1 0.590 1 0
3 0.686 0.518 0.389 0.385
4 0.241 -0.266 0.646 1
5 0.872 0.676 0.412 0.956
However, in df |> mutate, the line b = (b - min(a, na.rm = TRUE...)) should read b = (b - min(b, na.rm = TRUE...)). This is a super easy error to make with my current copy/pasta/edit workflow, but writing a function for this series of tasks will eliminate the possibility for that particular error.
To visualize the repetitive components, we can pull the code out of mutate() and put each command on its own line: