Skip to content

Commit

Permalink
Add comparison of named vs passed cols for embracing
Browse files Browse the repository at this point in the history
This distinction is important and used in our exercise
  • Loading branch information
ethanwhite committed Oct 8, 2024
1 parent 7a64762 commit e0de003
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions materials/functions-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ make_plot(surveys, hindfoot_length, "Hindfoot Length [mm]")
make_plot(surveys, weight, "Weight [g]")
```

* Only need to embrace column names that we are passing as arguments
* If we assume there is a column with some name in the data frame it doesn't need embracing

```r
library(tidyr)

create_time_series <- function(df, column){
time_series <- df |>
drop_na({{ column }}) |> # column is a variable for a column name, needs {{}}
group_by(year) |> # year is an actual column in the data frame, no {{}}
summarize(avg_size = mean({{ column }}))
return(time_series)
}

create_time_series(surveys, weight)
create_time_series(surveys, hindfoot_length)
```

> Do [Writing Tidyverse Functions]({{ site.baseurl }}/exercises/Functions-writing-tidyverse-functions-R).
### Code design with functions
Expand Down

0 comments on commit e0de003

Please sign in to comment.