Skip to content

Commit

Permalink
Update 03-functional-programming.qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues authored Nov 23, 2023
1 parent 5bab47c commit 038db33
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions 03-functional-programming.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,55 @@ my_func(c(1, 8, 1, NA, 8), mean, na.rm = TRUE)
- functions that take data (and the data's columns) as arguments [(section 7.6)](https://b-rodrigues.github.io/modern_R/defining-your-own-functions.html#functions-that-take-columns-of-data-as-arguments);

```{webr-r}
simple_function <- function(dataset, col_name){
simple_function <- function(dataset, col_name, var_name, fn_name){
dataset %>%
group_by({{col_name}}) %>%
summarise(mean_mpg = mean(mpg))
group_by(across({{col_name}})) %>%
summarise("{{fn_name}}_{{var_name}}" := {{fn_name}}({{var_name}}))
}
# Group by one column
simple_function(mtcars, vs, mpg, sd)
# Group by several columns
simple_function(mtcars, c(am, vs), mpg, sd)
# Even more general: group by any columns and apply any number of functions
simple_function <- function(dataset, cols, vars, fns){
dataset %>%
group_by(across({{cols}})) %>%
summarise(across({{vars}}, fns, .names = "{.fn}_{.col}"))
}
simple_function(mtcars, c(am, vs), c(mpg, hp), list("mean" = mean, "sd" = sd))
simple_function(mtcars, cyl)
```

Read more about it [here](https://dplyr.tidyverse.org/reference/across.html) and
[here](https://dplyr.tidyverse.org/articles/programming.html).


## Functional programming

You should ideally work through the whole of chapter 7, and then tackle [chapter
8](https://b-rodrigues.github.io/modern_R/functional-programming.html). What's
important there are:

- `purrr::map()`, `purrr::reduce()` (sections [8.3.1](https://b-rodrigues.github.io/modern_R/functional-programming.html#doing-away-with-loops-the-map-family-of-functions) and [8.3.2](https://b-rodrigues.github.io/modern_R/functional-programming.html#reducing-with-purrr))

Apply a function to each element of a vector or list:

```{webr-r}
map(seq(1:10), sqrt)
```

(`lapply()` is a base function that works similarly to `purrr::map()`)

Reduce a vector to a single element by iteratively applying a function:

```{webr-r}
reduce(seq(1:10), `+`)
```

- And list based workflows (section [8.4](https://b-rodrigues.github.io/modern_R/functional-programming.html#functional-programming-and-plotting))

## Further reading
Expand Down

0 comments on commit 038db33

Please sign in to comment.