Skip to content

Commit

Permalink
Merge pull request #1084 from ethanwhite/tidy-funcs
Browse files Browse the repository at this point in the history
Add comparison of named vs passed cols for embracing
  • Loading branch information
ethanwhite authored Oct 8, 2024
2 parents 7a64762 + e0de003 commit 3222a74
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 3222a74

Please sign in to comment.