Skip to content

Commit

Permalink
minor changes to spacing iter01
Browse files Browse the repository at this point in the history
  • Loading branch information
3mmaRand committed Sep 4, 2023
1 parent 58bb204 commit dce6534
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions 03-iteration-01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ lksdjfjksdf

Iteration is different in R because much is an inherent part of the language.

. . .
. . .

For example, if

Expand All @@ -57,16 +57,18 @@ Then
#| eval: false
2 * nums
```

is

. . .
. . .

``` r
[1] 6 2 12 8
```

and NOT

. . .
. . .

``` r
[1] 6 2 12 8 6 2 12 8
Expand Down Expand Up @@ -104,7 +106,7 @@ For examples
usethis::create_project("workshop-iterations")
```

. . .
. . .

🎬 Load packages:

Expand Down Expand Up @@ -161,7 +163,6 @@ penguins |>

`across(.cols, .fns, .names)`


3 important arguments

- which columns you want to iterate over: `.cols = bill_length_mm:body_mass_g`
Expand Down Expand Up @@ -238,8 +239,7 @@ penguins |>

- thus function name is **not** followed by `()`

show the error - easy to forget in functions eg my_summary <- function(df, cols = where(is.numeric)){...}

show the error - easy to forget in functions eg my_summary \<- function(df, cols = where(is.numeric)){...}

## Include arguments

Expand All @@ -252,19 +252,15 @@ penguins |>

We get the NA because we have missing values[^1].


[^1]: There is no problem when we use `sd_error()` because we accounted for NA in our function definition


## Include arguments

`mean()` has an `na.rm` argument. How can we pass on `na.rm = TRUE`?

. . .
. . .

The solution is to create a new function that calls `mean()` with `na.rm = TRUE`

. . .
. . .

```{r}
penguins |>
Expand All @@ -276,36 +272,32 @@ penguins |>

Instead of writing `function` we can use `\`


```{r}
penguins |>
summarise(across(ends_with("mm"), \(x) mean(x, na.rm = TRUE)))
```

. . .

- This is called an **anonymous** or **lambda** function.
- This is called an **anonymous** or **lambda** function.

- It is anonymous because we do not give it a name with `<-`



## `.funs`: calling \> one function

How can we use more than one function across the columns?

```
```
penguins |>
summarise(across(ends_with("mm"), _MORE THAN ONE FUNCTION_))
```

. . .
. . .

by using a list

## `.funs`: calling \> 1 function


```{r}
penguins |>
summarise(across(ends_with("mm"), list(
Expand All @@ -317,7 +309,6 @@ penguins |>

the `_1` and `_2` are not very useful.


## `.funs`: calling \> one function

We can improve with naming the elements in the list
Expand Down Expand Up @@ -351,8 +342,8 @@ penguins |>

Especially important for mutate because column names are used in `across()`


Recall our `to_z()` function

```{r}
to_z <- function(x, middle = 1) {
trim = (1 - middle)/2
Expand All @@ -371,9 +362,8 @@ penguins |>
) |>
glimpse()
```
## `.names` to control output


## `.names` to control output

```{r}
penguins |>
Expand All @@ -385,7 +375,7 @@ penguins |>

Results go into existing columns

##
##

```{r}
penguins |>
Expand All @@ -398,7 +388,7 @@ penguins |>

## A note on dots in argument names

-
-

-

Expand All @@ -408,8 +398,6 @@ penguins |>

## `across()` in functions



```{r}
my_summary <- function(df, cols){
df |>
Expand All @@ -422,7 +410,6 @@ my_summary <- function(df, cols){
```


```{r}
my_summary(penguins, ends_with("mm"))
```
Expand All @@ -443,13 +430,12 @@ my_summary <- function(df, cols = where(is.numeric)){
my_summary(penguins)
```



## Your turn


## link between across and pivot_longer

??

## Summary
## Summary

[^1]: There is no problem when we use `sd_error()` because we accounted for NA in our function definition

0 comments on commit dce6534

Please sign in to comment.